组件注册:
Vue.component(组件名, {
data: 组件数据,
template: 组件模板内容
})
例:
Vue.component('button-counter', {
data: function(){
return {
count: 0
}
},
template: '<button> @click="counter"> 点击了{{count}}次></button>',
methods: {
counter: function(){
this.count++;
}
}
})
使用:
<div>
<button-counter></button-counter>
</div>
注意
data: 组件数据
,这里必须是一个函数,否则会报错- 组件命名方式
. 短横线方式:
Vue.component(‘my-button’, {})
驼峰方式:
Vue.component(‘MyComponent’, {})
短横线方式可以直接作为组件,
驼峰方式,需要将每个单词进行用短横线分割,例如: