本文實例講述了Vue開發之watch監聽數組、對象、變量操作。分享給大家供大家參考,具體如下:
1.普通的watch
data() { return { frontPoints: 0 }},watch: { frontPoints(newValue, oldValue) { console.log(newValue) }}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) } } }, deep: true }}3.對象的watch
data() { return { bet: { pokerState: 53, pokerHistory: 'local' } }},watch: { bet: { handler(newValue, oldValue) { console.log(newValue) }, deep: true }}4.對象的具體屬性的watch:
data() { return { bet: { pokerState: 53, pokerHistory: 'local' } }},computed: { pokerHistory() { return this.bet.pokerHistory }},watch: { pokerHistory(newValue, oldValue) { console.log(newValue) }}希望本文所述對大家vue.js程序設計有所幫助。
新聞熱點
疑難解答
圖片精選