vue 半自动化注册全局主键

:在组件目录下添加js文件内容如下

const COMPONENTS = [//组件列表
  {
    path: "./",
    components:[
      "Tab"
    ]
  }
]

export default function (fun){// 注册组件
  COMPONENTS.forEach(({path,components})=>{
    components.forEach(name=>{
      const {default:COMPONENT,default:{name:NAME}} = require(path+name+'.vue');
      fun(NAME || name,COMPONENT);
    })
  })
}

 步骤二: 在main.js引入该文件

vue2

// 导出的方法改为
export default {
    install(Vue){
        COMPONENTS.forEach(({path,components})=>{
            components.forEach(value=>{
                const NAME = require(path+value+'.vue').default.name || value;
                const ELEMENT = require(path+value+'.vue').default;
                Vue.component(NAME,ELEMENT);
                // console.log(NAME,ELEMENT);
            })
        })
    }
} 

// 在mian.js文件引用
import Vue from 'vue'
import wxUI from './components/index.js'
Vue.use(wxUI)

vue3

import components from './components';// 引入

const app = createApp(App);
app.use(router).mount('#app');

components(app.component);// 使用

猜你喜欢

转载自blog.csdn.net/YiYeZhiQiuDe_yzq/article/details/124636453