vue watch 监听对象的某个属性

作者: Darren 分类: vue 发布时间: 2019-07-25 15:39

对象属性的watch

data() {
  return {
    tradeData: {
      creator: 'anki',
      GoodsVoList: []
    }   
    }
},
watch: {
  tradeData: {
    handler(newValue, oldValue) {
      console.log(newValue)
    },
    deep: true
  }
}

只要tradeData中的属性发生变化(可被监测到的),便会执行handler函数;
如果想监测具体的属性变化,如GoodsVoList变化时,才执行handler函数,则可以利用计算属性computed做中间层

对象具体属性的watch

data() {
  return {
    tradeData: {
      creator: 'anki',
      GoodsVoList: []
    }   
    }
},
computed: {
  GoodsVoList() {
    return this.tradeData.GoodsVoList
  }
},
watch: {
  GoodsVoList(newValue, oldValue) {
    console.log(newValue)
  }
}

 

一条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注