Vue使用watch监听数组或对象
1、普通的watch
1 2 3 4 5 6 7 8 9 10 | data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console.log(newValue) } } |
2、数组的watch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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) } } }, deep: true } } |
3、对象的watch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | data() { return { bet: { pokerState: 53, pokerHistory: 'local' } } }, watch: { bet: { handler(newValue, oldValue) { console.log(newValue) }, deep: true } } |