Vue之watch監聽物件中某個屬性的方法
阿新 • • 發佈:2021-12-20
新建userinfo = { name: "小明", age: "18", }
vue中watch監聽name的方法
1. 可以結合計算屬性的方法實現
1 2 3 4 5 6 7 8 9 10 11 12 |
{
......
watch: {
nm () {
console.log( this .nm)
}
},
computed: {
nm () { return this .userinfo.name }
}
......
}
|
2. 可以通過配置 deep 為true實現
// 監聽物件的某個值 { ...... watch: { 'userinfo.name' () { console.log(this.nm) } } ...... } // 直接監聽整個屬性,消耗大 { ...... watch: { userinfo () { handler () { console.log(this.nm) }, deep: true } } ...... }
轉自:https://www.cnblogs.com/jingxuan-li/p/11817329.html