Use arrow function in vue computed does not work
You are facing this error because an arrow function wouldn't bind this
to the vue instance for which you are defining the computed property. The same would happen if you were to define methods
using an arrow function.
Don’t use arrow functions on an instance property or callback (e.g.
vm.$watch('a', newVal => this.myMethod()))
. As arrow functions are bound to the parent context, this will not be the Vue instance as you’d expect andthis.myMethod
will be undefined.
You can read about it here.
The arrow function lost the Vue component context. For your functions in methods
, computed
, watch
, etc., use the Object functions:
computed:{
switchRed() {
return {red: this.turnRed}
},
switchGreen() {
return {green: this.turnGreen}
},
switchBlue() {
return {blue: this.turnBlue}
}
}