【发布时间】:2018-08-17 02:37:17
【问题描述】:
我正在使用Vue.js 创建一个组件。
当我在任何 lifecycle hooks(created、mounted、updated 等)中引用 this 时,它的计算结果为 undefined:
mounted: () => {
console.log(this); // logs "undefined"
},
我的计算属性中也发生了同样的事情:
computed: {
foo: () => {
return this.bar + 1;
}
}
我收到以下错误:
未捕获的类型错误:无法读取未定义的属性“bar”
为什么在这些情况下this 的计算结果为undefined?
【问题讨论】:
标签: javascript vue.js this vuejs2