lau*_*kok 3 vue.js vuejs2 nuxt.js
为什么结果是不同的使用() =>和function()在mounted:
export default {
mounted: () => {
this.socket = 'something'
console.log('mounted')
},
methods: {
submitMessage() {
console.log(this.socket) // undefined
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用function():
export default {
mounted: function() {
this.socket = 'something'
console.log('mounted')
},
methods: {
submitMessage() {
console.log(this.socket) // something
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您不应该使用箭头函数来定义生命周期钩子,方法,...(例如mounted: () => this.socket++).原因是箭头函数绑定父上下文,因此这不是您期望的Vue实例,并且this.socket将是未定义的.
| 归档时间: |
|
| 查看次数: |
6806 次 |
| 最近记录: |