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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章