https://vuejs.org/v2/guide/components-registration.html
Global Registration
So far, we’ve only created components using Vue.component
:
Vue.component('my-component-name', {
// ... options ...
})
These components are globally registered. That means they can be used in the template of any root Vue instance (new Vue
) created after registration. For example:
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>
Local Registration
Global registration often isn’t ideal. For example, if you’re using a build system like Webpack, globally registering all components means that even if you stop using a component, it could still be included in your final build. This unnecessarily increases the amount of JavaScript your users have to download.
In these cases, you can define your components as plain JavaScript objects:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
Then define the components you’d like to use in a components
option:
new Vue({
el: '#app',
components: {
'component-a': ComponentA,
'component-b': ComponentB
}
})
Or if you’re using ES2015 modules, such as through Babel and Webpack, that might look more like:
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA
},
// ...
}
// Through babel and wepack compoenent A tag is still as the following
<component-a></component-a>
No comments:
Post a Comment