How does vue monitor property value changes?

How does vue monitor property value changes? For example, we need to monitor the changes of obj.a in data. You can monitor the changes of object properties in Vue like this:

watch: {   obj: {
   handler (newValue, oldValue) {
    console.log('obj changed')
   },
   deep: true
  }
 }

The deep attribute means deep traversal, but this will monitor all the changes of obj's attributes, which is not the effect we want, so make some changes:

watch: {  'obj.a': {
   handler (newName, oldName) {
    console.log('obj.a changed')
   }
  }
 }

There is another method, which can be implemented by computed, just:

computed: {
  a1(){
  return this.obj.a
  }
}

Use the characteristics of the calculated attribute to achieve, when the dependency changes, a new value will be recalculated.
 

Guess you like

Origin blog.csdn.net/cz_00001/article/details/112846227