美文网首页
10、vue3注册全局组件

10、vue3注册全局组件

作者: 蜗牛的学习方法 | 来源:发表于2023-02-21 15:12 被阅读0次

在项目里面components里面新建了一个Empty/index.vue组件,内容如下

// src/components/Empty/index.vue
<template>
  <div>这是一个全局组件</div>
</template>

1、全局注册使用第一种方式

在main.ts引入

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import Empty from './components/Empty/index.vue'

const app=createApp(App)

app.component('Empty',Empty)
app.mount('#app')

2、全局引入第二种方式

这种方式可以把多个组件的注册放在一起,避免都放在main.ts里面引起观感不好。
src/components/index.ts里面直接导出使用

// src/components/index.ts
import Empty from './Empty/index.vue'

export const useGlobalComponent = (app: any) => {
  app.component('Empty', Empty)
}

//main.ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import {useGlobalComponent} from './components/index'

const app=createApp(App)

useGlobalComponent(app)
app.mount('#app')

3、全局引入第三种方式

写法一

这种是根据components下面的文件夹名字来自动生成组件注册

// src/components/index.ts
const components:any= import.meta.globEager('./*/index.vue')
//提示被废弃就用下面这个
// const components:any= import.meta.glob('./*/index.vue',{eager:true})

export const useGlobalComponent = (app: any) => {
  const componentsData = Object.keys(components).map(key => {
    const template = components[key].default
    const nameData = key.split('/')
    return {name: nameData.length ? nameData[1] : null, template}
  })
  componentsData.forEach((item:any) => {
    if(item.name){
      app.component(item.name, item.template)
    }else{
      console.error("component registered failed", item)
    }
  })
}
//main.ts同上面的用法
...

写法二

// src/components/index.ts
const components:any= import.meta.globEager('./*/index.vue')
//提示被废弃就用下面这个
// const components:any= import.meta.glob('./*/index.vue',{eager:true})

export default {
  install: (app: any) => {
    const componentsData = Object.keys(components).map(key => {
      const template = components[key].default
      const nameData = key.split('/')
      return {name: nameData.length ? nameData[1] : null, template}
    })
    componentsData.forEach((item:any) => {
      if(item.name){
        app.component(item.name, item.template)
      }else{
        console.error("component registered failed", item)
      }
    })
  }
}

// main.ts
import { createApp } from 'vue'
import Antd from 'ant-design-vue';
import './style.css'
import App from './App.vue'
import useGlobalComponent from './components/index' //看这

const app=createApp(App)
app.use(useGlobalComponent)  //看这 要用use就要用install,注册调用(app.use,会调用上面的install,并传参数)
app.mount('#app')

相关文章

  • 10、vue3注册全局组件

    在项目里面components里面新建了一个Empty/index.vue组件,内容如下 1、全局注册使用第一种方...

  • 深入了解组件

    组件注册   组件注册分为全局注册和局部注册  全局注册   局部注册   基础组件的自动化全局注册可能你的许多组...

  • vue组件

    关于vue组件的总结 注册组件 vue中组件的注册分为两种: 全局注册 局部注册 全局注册 全局注册的组件在任何v...

  • Vue 之 组件

    组件注册 组件注册分为两种: 全局注册 和 局部注册 全局注册:全局注册的行为必须在根 Vue 实例 ...

  • 注册组件的语法糖写法

    1.全局注册组件语法糖 全局组件不用语法糖的构建过程:生成组件构造器 注册组件 使用组件 全局注册组件的语法糖,省...

  • 组件

    注册组件 注册组件分为全局注册和局部注册。 全局注册 step1.用Vue.component()方法定义一个组件...

  • vue3

    最近有项目用vue3来做的,这边做一下记录,便于以后回忆: 全局注册组件: https://www.yuque.c...

  • vue-echarts全局组件使用

    在入口main.js 注册全局组件 引入 import 'echarts' 注册全局组件 Vue.componen...

  • vue3 批量注册全局组件(共用组件)

    vue2中引入共用组件 main.js 中引入 就可以了! vue3中引入共用组件 main.js 中引入 就可以...

  • Vue 组件全局注册和局部注册使用及原理

    Vue在注册组件时有两种方式,全局注册和局部注册全局注册的话我们可以在任意组件中使用注册的组件,而局部注册的话我们...

网友评论

      本文标题:10、vue3注册全局组件

      本文链接:https://www.haomeiwen.com/subject/nnnnkdtx.html