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>

浙公网安备 33010602011771号