Use arrow function in vue computed does not work

前端 未结 4 1248
栀梦
栀梦 2020-11-22 07:44

I am learning Vue and facing a problem while using arrow function in computed property.

My original code works fine (See snippet below).

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 08:06

    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;
    }
    
    

提交回复
热议问题