Vue.use()和install的小知识

Vue.use()文档地址

我们自己在注册组件时候,需要先引入,然后在components中注册组件,但是使用Element UI组件时不需要注册,只需要Vue.use()一下就可以了,这是因为我们在写入Vue.use(ElementUI)时,他们帮我们做了一些事情;

1.常规写法

import componentA from './component/componentA '
components:{
    
    
componentA 
}

2.ElementUI写法

import Vue from 'vue';
import ElementUI from 'element-ui';
Vue.use(ElementUI);// 使用Vue.use即可

实际上我们在执行Vue.use方法时,对象内的install方法就会被执行一次。

使用示例:
在src/button/index.js中写入:

import DYnput from './components/button.vue'

const components = [
  DYbutton
]

const DYUI = {
    
    
  install(Vue) {
    
    //注:这里会将Vue类传过来
    // 注册组件,注如有多个组件时可以遍历注册。
    components.forEach(component => {
    
    
      Vue.component(component.name, component)
    })
  }
}

export default DYUI 

main.js中引入:

import DYUI from './modules/dy-ui'
Vue.use(DYUI)

猜你喜欢

转载自blog.csdn.net/qq_42931285/article/details/126700453