学习Vue中的一些注意事项

在某些时候不能使用箭头函数

不能在选项属性或者回调上使用箭头函数, 比如
created: () =>console.log(this.a) 或者vm.$watch('a',newValue => this.myMethod())
因为在Vue中箭头函数是和父级上下文绑定在一起的,也就是说this并不是Vue实例,所以会经常导致
Uncaught TypeError: Cannot read property of undefinedUncaught TypeError: this.myMethod is not a function 之类的错误。

补充一下: 像vm.$watch 都是属性。

猜你喜欢

转载自blog.csdn.net/zsm4623/article/details/87360025