vue監聽對象及對象屬性

監聽整個對象,使用watch就行

export default {
    data() {
        return {
            a: {
                b: 1,
                c: 2
            }
        }
    },
    watch() {
        a: {
            handler(newVal, oldVal) {
                console.log('監聽a整個對象的變化');
            },
            deep: true
        }
    }
}

監聽對象中具體屬性的變化,須要使用watch配合computed

export default {
    data() {
        return {
            a: {
                b: 1,
                c: 2
            }
        }
    },
    watch() {
        bChange() {
            console.log('監聽a對象中b屬性的變化');
        }
    },
    computed: {
        bChange() {
            return this.a.b;
        }
    }
}
相關文章
相關標籤/搜索