sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Vue2.0 no-side-effects-in-computed-properties WARNING处理
点击原文查看动态效果 https://blog.csdn.net/elie_yang/article/details/80472640

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

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

  }

编译警告信息如下:


Unexpected side effect in "listShow" computed property 

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

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

    },

...

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

  }


</article>
posted on 2022-03-22 22:43  sunny123456  阅读(197)  评论(0编辑  收藏  举报