全局一次性引用写好的组件

全局一次性引用写好的组件

我们写好了组件,接下去肯定还要引入和使用。但是你写的组件一旦多起来,在每一个个地方都需要去引用是一件很麻烦的事情,所以我们将一次性全局引入:

新建 components.js 文件 , 引入所有组件

important componentA from './componentA
important componentB from './componentB
important Vue from 'vue

const components = [componentA, componentB]

const install = function (value, opts = {}){
  components.map(component => {
    Vue.component(component.name, component)
  })
}

export defau;t install

在 main.js 进行导入

import components from './components.js'

Vue.use(components)

如此,便可在任意位置进行使用组件。

    原文作者:小刀
    原文地址: https://segmentfault.com/a/1190000012501060
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞