无法访问观察者内部的数据变量,内部 .map()
Can't access data variable inside watcher, inside .map()
我的根是这样的。我在数据和手表中定义了类型和历史变量,我试图访问另一个变量而不是我正在观看的变量。尝试在 .map()
外部访问历史变量是有效的,但是在 .map()
returns undefined
内部使用它
new Vue({
el: '#root',
data: {
types: {},
history: {}
}
watch: {
types: {
handler(obj) {
console.log(this.history) // works
types.map(function(val) {
console.log(this.history) // undefined
})
}
}
}
}
因为您使用的是 CDN,所以您需要将 this
绑定到您的函数。尝试以下操作:
types.map(function(val) {
console.log(this.history)
}).bind(this);
我的根是这样的。我在数据和手表中定义了类型和历史变量,我试图访问另一个变量而不是我正在观看的变量。尝试在 .map()
外部访问历史变量是有效的,但是在 .map()
returns undefined
new Vue({
el: '#root',
data: {
types: {},
history: {}
}
watch: {
types: {
handler(obj) {
console.log(this.history) // works
types.map(function(val) {
console.log(this.history) // undefined
})
}
}
}
}
因为您使用的是 CDN,所以您需要将 this
绑定到您的函数。尝试以下操作:
types.map(function(val) {
console.log(this.history)
}).bind(this);