1.vue中data必须是一个函数。
虽然在教程的例子中大多直接写成这样:
1 | data: { |
但是如果在组件中这样写会导致数据影响到所有复用组件,所以应该改为这样:
1 | data(){ |
官网的例子可以很好的说明这个问题:https://cn.vuejs.org/v2/guide/components.html
1 | Vue.component('button-counter', { |
1 | <div id="components-demo"> |
2.组件中的template必须被html标签包裹。
上面的例子,如果去掉标签就不显示了,比如像这样:
1 | template: 'You clicked me {/{count/}} times.' |
3.注意箭头函数()=>{} 与function()的区别。
箭头函数的this永远指向定义函数的环境,而function中指向的是调用该函数的对象。比如官网上的这个例子:
1 | new Vue({ |