注册全局组件 也可注册局部组件。
// 注册全局
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
// 创建根实例
new Vue({
el: '#example',
data: function () { //为什么要使用 data:function 然后return 返回 原因是这样不会讲data里的数据变成全局,每个组件用到都会改变
return { a: 1 }
}
})
//局部
var Child = {
template: '<div>A custom component!</div>'
}
new Vue({
// ...
components: {
// <my-component> 将只在父组件模板中可用
'my-component': Child
}
})
除了注册的组件外还有很多内置组件是无需注册的。直接用就好了。可以参考transition