Vue报错 Unexpected side effect in "*" computed property

export default {
    computed: {
        liveShow() {
            if (!this.liveIsClose) {
                this.$nextTick(() => {
                    // someCode
                }
                return true;
            } else {
                return false;
            }            
        }
    }
}

这时候会提示这样写有问题。计算属性只要单纯的运算,依赖某些值,得到某个值。不要做其他的操作,赋值,修改dom等。

真的需要操作就放到watch里面。

export default {
    computed: {
        liveShow() {
            if (!this.liveIsClose) {
                return true;
            } else {
                return false;
            }            
        }
    },
    watch() {
        liveShow(val) {
            if(val) {
                // someCode
            }
        }
    }
}
原文地址:https://www.cnblogs.com/maopixin/p/13300842.html