站长资源网络编程

vue监听对象及对象属性问题

整理:jimmy2026/7/31浏览2
简介监听整个对象,使用watch就行export default {data() {return {a: {b: 1,c: 2}}},watch() {a: {handler(newVal, oldVal) {console.log('监听a整个对象的变化');},deep: true}}}监听对象中

监听整个对象,使用watch就行

export default {
  data() {
    return {
      a: {
        b: 1,
        c: 2
      }
    }
  },
  watch() {
    a: {
      handler(newVal, oldVal) {
        console.log('监听a整个对象的变化');
      },
      deep: true
    }
  }
}

监听对象中具体属性的变化,需要使用watch配合computed

export default {
  data() {
    return {
      a: {
        b: 1,
        c: 2
      }
    }
  },
  watch() {
    bChange() {
      console.log('监听a对象中b属性的变化');
    }
  },
  computed: {
    bChange() {
      return this.a.b;
    }
  }
}