handler:監聽數組或對象的屬性時用到的方法
deep:深度監聽,為了發現對象內部值的變化,可以在選項參數中指定 deep:true 。注意監聽數組的變動不需要這么做。
immediate: 在選項參數中指定 immediate: true 將立即以表達式的當前值觸發回調
tips: 只要bet中的屬性發生變化(可被監測到的),便會執行handler函數;如果想監測具體的屬性變化,如pokerHistory變化時,才執行handler函數,則可以利用計算屬性computed做中間層。
1、普通的watch
按 Ctrl+C 復制代碼
data() {
return {
frontPoints: 0
}
},
watch: {
frontPoints(newValue, oldValue) {
console.log(newValue)
}
}
按 Ctrl+C 復制代碼
2、數組的watch
data() { return { winChips: new Array(11).fill(0) } }, watch: { winChips: { handler(newValue, oldValue) { for (let i = 0; i < newValue.length; i++) { if (oldValue[i] != newValue[i]) { console.log(newValue) } } }, } }