1.普通的watch
data() {
return {
frontPoints: 0
}
},
watch: {
frontPoints(newValue, oldValue) {
console.log(newValue)
}
}
2.對象屬性的watch
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
watch: {
bet: {
handler(newValue, oldValue) {
console.log(newValue)
},
deep: true
}
}
tips: 只要bet中的屬性發(fā)生變化(可被監(jiān)測到的)系草,便會執(zhí)行handler函數(shù)通熄;
如果想監(jiān)測具體的屬性變化,如pokerHistory變化時(shí)找都,才執(zhí)行handler函數(shù)唇辨,則可以利用計(jì)算屬性computed做中間層。
事例如下:
3.對象具體屬性的watch
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
computed: {
pokerHistory() {
return this.bet.pokerHistory
}
},
watch: {
pokerHistory(newValue, oldValue) {
console.log(newValue)
}
}