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

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

  }

编译警告信息如下:

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

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

  }

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2021-07-27
  • 2022-02-12
猜你喜欢
  • 2021-11-22
  • 2021-11-05
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案