灵心如玉,守一生无惧|

SadicZhou

园龄:3年2个月粉丝:7关注:4

vue2之对象属性的监听

vue2之对象属性的监听的一些总结

对象属性监听的两种方法:

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中的属性发生变化(可被监测到的),便会执行handler函数;
如果想监测具体的属性变化,如pokerHistory变化时,才执行handler函数,则可以利用计算属性computed做中间层。
事例如下:

3.对象具体属性的watch

 
复制代码
data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    }
    }
},
computed: {
  pokerHistory() {
    return this.bet.pokerHistory
  }
},
watch: {
  pokerHistory(newValue, oldValue) {
    console.log(newValue)
  }
}
复制代码

对象具体属性的watch可以直接用引号把属性括起来,就可以实现对象中特定属性的监听事件:

复制代码
data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    }
    }
},
watch: {
  'bet.pokerHistory'(newValue, oldValue) {
    console.log(newValue)
  }
}
复制代码

 

本文作者:SadicZhou

本文链接:https://www.cnblogs.com/SadicZhou/p/17244863.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   SadicZhou  阅读(1272)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 尚好的青春 孙燕姿
  2. 2 孙燕姿
  3. 3 克卜勒 孙燕姿
克卜勒 - 孙燕姿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.