我想给我的同名店打电话:
methods: {
...mapActions('Modal', [
'toggleActive',
]),
close: () => {
this.toggleActive();
}这将导致错误:
Uncaught TypeError: Cannot read property 'toggleActive' of undefined从事以下工作:
close: function() {
this.toggleActive();
}如何在vue/vuex中使用ES6函数语法?
发布于 2017-07-27 08:54:20
你在使用箭头函数。箭头函数在定义它们的上下文中关闭在this上,而不是在调用时设置为function函数。例如:
// Whatever `this` means here
var o = {
foo: () => {
// ...is what `this` means here when `foo` is called
}
};您可能只想使用方法语法来代替:
methods: {
// (I'm using a simple method for toggleActive just for clarity; if you're
// transpiling with something that understands rest notation for
// objects, your `mapActions` thing is probably fine *provided* that
// the functions it creates aren't also arrow functions
toggleActive() {
// ...
},
close() {
ths.toggleActive();
}
};请注意,这取决于所有常见的this described in this question's answers。
https://stackoverflow.com/questions/45345784
复制相似问题