Examples of components are reusable Vue
data options for a component must be a function
Registered two types of components: global registration and partial registration
Global registration:
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>
全局注册往往是不够理想的。比如,如果你使用一个像 webpack 这样的构建系统,全局注册所有的组件意味着即便你已经不再使用一个组件了,它仍然会被包含在你最终的构建结果中。这造成了用户下载的 JavaScript 的无谓的增加。
局部注册:
In these cases, you can define the components through a common JavaScript objects:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
Then components
define the components you want to use options:
new Vue({
el: '#app',
components: {
'component-a': ComponentA, 'component-b': ComponentB } })
Note that components in its local register subassembly unusable . For example, if you want ComponentA
in ComponentB
the available, then you need to write:
var ComponentA = { /* ... */ }
var ComponentB = {
components: {
'component-a': ComponentA }, // ... }
Component name: all lowercase letters and must contain a hyphen (strongly recommended)