SoFunction
Updated on 2025-03-02

Vue listening to objects and object properties issues

Listen to the entire object, just use watch

export default {
  data() {
    return {
      a: {
        b: 1,
        c: 2
      }
    }
  },
  watch() {
    a: {
      handler(newVal, oldVal) {
        ('Listen to the changes of a whole object');
      },
      deep: true
    }
  }
}

Listen to changes in specific attributes in the object, you need to use watch and computed

export default {
  data() {
    return {
      a: {
        b: 1,
        c: 2
      }
    }
  },
  watch() {
    bChange() {
      ('Listen to the changes in the b attribute in the a object');
    }
  },
  computed: {
    bChange() {
      return ;
    }
  }
}