I am learning Vue and facing a problem while using arrow function in computed property.
My original code works fine (See snippet below).
When creating computed you do not use =>
, you should just use switchRed () {...
Take a look at snippet. Works as it should.
It applies to all computed,method, watchers etc.
new Vue({
el: '#app',
data: {
turnRed: false,
turnGreen: false,
turnBlue: false
},
computed:{
switchRed () {
return {red: this.turnRed}
},
switchGreen () {
return {green: this.turnGreen}
},
switchBlue () {
return {blue: this.turnBlue}
}
}
});
.demo{
width: 100px;
height: 100px;
background-color: gray;
display: inline-block;
margin: 10px;
}
.red{
background-color: red;
}
.green{
background-color: green;
}
.blue{
background-color: blue;
}