vue组件全局注册和插件注册的区别

296 阅读1分钟

1.全局注册

在main.js中直接注册

import DatePicker from "@/components/datePicker/datePicker.vue";
Vue.component('DatePicker',DatePicker)

缺点: 如果我们需要注册的全局组件非常多,那么需要一个一个引入,然后分别调用Vue.componet方法,main.js文件会变得很大很臃肿,不好维护

2.插件注册

①在components文件夹下新建index.js文件

import DatePicker from "@/components/datePicker/datePicker.vue";
import modalSure from "@/components/modal/modalSure/modalSure.vue";
export default{
    install(Vue){
        Vue.component('DatePicker',DatePicker)
        Vue.component('modalSure',modalSure)
    }
}

②在main.js中注册插件入口

import Components from '@/components'

Vue.use(Components)

好处:下次需要注册组件的时候,只需要在index.js添加对应的组件即可,易于维护