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

作者: 简小咖 | 来源:发表于2022-07-07 16:34 被阅读0次

vue2中引入共用组件

\\ components/index.js
import Vue from 'vue'
import LayoutContent from './layout/LayoutContent'
import FormPart from './form-part'
Vue.component(LayoutContent.name, LayoutContent)
Vue.component(FormPart.name, FormPart)

main.js 中引入

import './components'

就可以了!

vue3中引入共用组件

\\ components/index.js
import FormPart from './form-part/index.vue'
import LayoutContent from "./layout/LayoutContent.vue";

export default {
  install(app) {
    app.component(FormPart.name, FormPart);
    app.component(LayoutContent.name, LayoutContent);
  },
};

main.js 中引入

import  Components from '@/components'
const app = createApp(App)
app.use(Components)
app.mount('#app')

就可以了!

批量注册

vue2的时候

/* Components */
import Vue from 'vue'
const components = require.context('./', true, /index\.vue$/);
components.keys().forEach(key => {
  let component = components(key).default;
  Vue.component(component.name, component)
})

vue3 用vite不能使用require.context,要使用import.meta.globEage

/* Components */
const components= import.meta.globEager('./*/index.vue')
export default {
  install(app) {
    Object.keys(components).forEach(key => {
    let component = components[key].default
    app.component(component.name, component)
  })
  },
};

相关文章

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

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

  • vue组件

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

  • "重要" Vue3 批量注册全局组件

    大致步骤: 1,新建.js文件,使用require提供的函数context加载某一个目录下所有的.vue后缀的文件...

  • 深入了解组件

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

  • 注册组件的语法糖写法

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

  • Vue 之 组件

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

  • vue批量注册全局组件

    在公共组件中多次引入的问题。如某个组件使用频次在两次以上,建议注册为全局组件,以便后续开发便捷使用,防止在父组件中...

  • 组件

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

  • vue组件系统

    除了根组件之外的全局组件和局部组件的data数据必须是函数 根组件 全局组件: 全局组件注册方式:Vue.comp...

  • vue 组件循环引用

    Vue.component 全局注册组件时,组件循环引用可以解开。 当组件不是全局注册的时候 我们使用递归组件需要...

网友评论

    本文标题:vue3 批量注册全局组件(共用组件)

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