Vue组件注册

1.全局注册
   Vue.component('组件名', { /* ... */ })
        组件在全局注册之后,我们可以在任何新创建的Vue根实例下使用:
   Vue.component('component-a', { /* ... */ })
   Vue.component('component-b', { /* ... */ })
   Vue.component('component-c', { /* ... */ })
   new Vue({ el: '#app' })
 <div id="app">
    <component-a></component-a>
    <component-b></component-b>
    <component-c></component-c>  //在三个组件内部可以相互使用
 </div>
 
2.局部注册
   在Vue实例中,我们可以使用components对象来注册组件:
   var ComponentA = { /* ... */ }
   var ComponentB = { /* ... */ }
   new Vue({
     el: '#app',
     components: {
        'component-a': ComponentA,    
        'component-b': ComponentB
     }
   })
 
 在components中,每个对象的属性就是组件的名字,属性值就是组件对象;在局部组件中,组件间不能相互使用,如果想在ComponentA中使用ComponentB,那么,我们要这样写:
 var ComponentA = { /* ... */ }
 var ComponentB = {
   components: {
     'component-a': ComponentA
   },
  // ...
 }
模块系统 
当我们使用模块系统创建好一个个“.vue”的组件之后,我们可以通过import将它引入到别的模块中使用:
import ComponentA from './ComponentA'   //首先,我们将其引入
export default {
  components: {
    ComponentA                                    //这里将它局部注册
  },
  // ...
}
但是,有时候这样会显得很麻烦,当我们有一些通用的组件的时候,我们可以让它全局注册:
如果使用了 webpack (或在内部使用了 webpack 的 Vue CLI 3+),那么就可以使用 require.context 只全局注册这些非常通用的基础组件。
posted @ 2019-11-16 22:45  淡薄幽清  阅读(57)  评论(0)    收藏  举报