Vue开发之watch监听数组、对象、变量操作分析
程序员文章站
2023-11-29 13:44:52
本文实例讲述了vue开发之watch监听数组、对象、变量操作。分享给大家供大家参考,具体如下:
1.普通的watch
data() {
return {...
本文实例讲述了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程序设计有所帮助。