vue--》如何使用wacth監聽對象的屬性變化?


  在開發過程中,我們經常需要監聽watch監聽一個對象的變化,但是如何來實現     監聽對象中屬性的變化呢?

  先回顧一下如何監聽整個對象的變化,使用watch就行了

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

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

export default {
    data() {
        return {
            a: {
                b: 1,
                c: 2
            }
        }
    },
    watch() {
        bChange() {
            console.log('監聽a對象中b屬性的變化');
        }
    },
    computed: {
        bChange() {
            return this.a.b;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM