广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue3中注册全局的组件,并在TS中添加全局组件提示方式
  • 813
分享到

Vue3中注册全局的组件,并在TS中添加全局组件提示方式

2024-04-02 19:04:59 813人浏览 薄情痞子
摘要

目录vue3中注册全局的组件Vue3踩坑--全局注册组件我的框架:vue3+vite+ts+naiveUIVue3中注册全局的组件 1. 在src/components中新建inde

Vue3中注册全局的组件

1. 在src/components中新建index.ts用来将所有需要全局注册的组件导入

✨: 如果使用的是 js 可以删除类型校验

import type { Component } from 'vue'
import SvgIcon from './SvgIcon/index.vue'

// ✨如果使用的是 JS 可以删除类型校验
const components: {
  [propName: string]: Component
} = {
  SvgIcon
}
export default components

2. 在main.ts中导入

✨这里使用循环的方式, 将每个全局组件进行注册

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.CSS' // 基于断点的隐藏类 Element 额外提供了一系列类名,用于在某些条件下隐藏元素
import App from './App.vue'
import router from './router'
import { store, key } from './store'

import globalComponent from '@/components/index'

const app = createApp(App)

app.use(store, key).use(router).use(ElementPlus).mount('#app')

// 注册全局的组件
for (const componentItme in globalComponent) {
  app.component(componentItme, globalComponent[componentItme])
}

3. 如果使用TS编写,还需要在和main.ts同级的目录, 创建一个components.d.ts, 用来处理组件引入报错的问题和添加组件提示

import SvgIcon from '@/components/SvgIcon/index.vue'
declare module '@vue/runtime-core' {
  export interface GlobalComponents {
    SvgIcon: typeof SvgIcon
  }
}

4. 最后直接导入即可

Vue3踩坑--全局注册组件

我的框架:vue3+vite+ts+naiveUI

步骤一:

创建一个loading文件夹

index.vue

index.ts

//loading/index.vue
<script lang="ts">
import { NSpace, NSpin } from 'naive-ui'
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'HsNavLoading',
  components: {
    NSpace,
    NSpin
  }
})
//loading/index.ts
import { App } from 'vue'
import loading from './index.vue'

export default {
  install(app: App) {
    app.component(loading.name, loading)
  }
}

步骤二:

components文件夹下创建index.ts一次引入多个全局组件并导出

//components/index.ts
import { App } from 'vue'
import loading from '@/components/Loading/index'

const components = [loading]
export default {
  install(app: App) {
    components.map((item) => {
      app.use(item)
    })
  }
}

步骤三:

main.js引入

//main.js
import { createApp } from 'vue'
import App from './App.vue'
import components from '@/components/index'
const app = createApp(App)
app.use(router).mount('#app')
app.use(components)

步骤四:

在单页面中使用

<hsNavLoading :show="isLoading" />

遇到的坑:

一开始loading/index.vue使用setup语法糖,通过 defineExpose 来导出name

报错

Failed to resolve component: hsNavLoading If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

<script lang="ts" setup>
import { NSpace, NSpin } from 'naive-ui'
defineExpose({
   name: 'HsNavLoading'
})

</script>

解决办法:如步骤一,把语法糖换掉

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Vue3中注册全局的组件,并在TS中添加全局组件提示方式

本文链接: https://www.lsjlt.com/news/165476.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue3中注册全局的组件,并在TS中添加全局组件提示方式
    目录Vue3中注册全局的组件Vue3踩坑--全局注册组件我的框架:vue3+vite+ts+naiveUIVue3中注册全局的组件 1. 在src/components中新建inde...
    99+
    2022-11-13
  • Vue中怎么全局注册组件并引用
    这篇文章将为大家详细讲解有关Vue中怎么全局注册组件并引用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1、正则判断路径以及文件名,获取全部组件并全局注册(...
    99+
    2022-10-19
  • vue3中的elementPlus全局组件中文转换方式
    目录elementPlus 全局组件中文转换elementPlus 设置默认语言为中文elementPlus 全局组件中文转换 在项目中使用日期下拉框发现是英文的,需要全局改成中文样...
    99+
    2022-11-13
  • 在Vue2中注册全局组件的两种方法详解
    第一种:在main.js中直接注册 //引入 import FixedTop from '@/components/FixedTop //注册为全局组件 Vue.componet('...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作