Vue2.0 no-side-effects-in-computed-properties WARNING处理

V2.0学习《高仿饿了么》过程中,对照视频代码编写如下代码:

listShow() {
        if (!this.totalCount) {
          this.fold = true;
          return false;
        }
        let show = !this.fold;
        return show;
      }

  }

编译警告信息如下:


Unexpected side effect in "listShow" computed property 

个人理解计算属性内不应该对属性值做变更,解决这个问题的做法之一是使用watch监听:

computed: {
    listShow () {
      if (!this.totalCount) {
        // this.collapsed = false;
        return false;
      }
      if (this.totalCount > 0 && !this.collapsed) {
        return true;
      }
      return false;

    },

...

watch: {
    selectedFoods (newFoods, oldFoods) {
      if (newFoods.length === 0) {
        this.collapsed = true;
      }
    }

  }


猜你喜欢

转载自blog.csdn.net/elie_yang/article/details/80472640
今日推荐