Use arrow function in vue computed does not work

前端 未结 4 1122
栀梦
栀梦 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;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.4/vue.js"></script>
    <div id="app">
      <div class="demo" @click="turnRed = !turnRed" :class="switchRed"></div>
      <div class="demo" @click="turnGreen = !turnGreen" :class="switchGreen"></div>
      <div class="demo" @click="turnBlue = !turnBlue" :class="switchBlue"></div>
    </div>

    0 讨论(0)
  • 2020-11-22 08:10

    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 and this.myMethod will be undefined.

    You can read about it here.

    0 讨论(0)
  • 2020-11-22 08:14

    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}
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:20

    And why not something simpler like this?

    new Vue({
      el: '#app',
      data: {
        turnRed: false,
        turnGreen: false,
        turnBlue: false
      },
      methods:{
      	toggle (color) {
        	this[`turn${color}`] = !this[`turn${color}`];
        }
      }
    });
    .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;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.4/vue.js"></script>
    <div id="app">
      <div class="demo" @click="toggle('Red')" :class="{red:turnRed}"></div>
      <div class="demo" @click="toggle('Green')" :class="{green: turnGreen}"></div>
      <div class="demo" @click="toggle('Blue')" :class="{blue: turnBlue}"></div>
    </div>

    0 讨论(0)
提交回复
热议问题