Experience on pit

I just started listening for object properties in the array where newValue is the newValue that changed, and oldValue is the oldValue that changed

watch:{
  'listMenu[4].value': {handler(newValue, oldValue){console.log (newValue)}}Copy the code

Watcher only accepts simple dot-delimited paths. For full control, use a function instead

The solution

Listen for object property changes in an array

Use the following two methods:

// The first method:
created() {
  this.$watch(
    () = > this.listMenu[4].value,
    function(val, old) {})}// Second method:
computed: {watchInputData(){ return this.listMenu[4].value }
},
watch: {watchInputData() {
    // You can use this command}}Copy the code

Listen for an object and its properties

/* Ordinary watch cannot listen for changes in object properties, only when data changes in data can listen for changes. So you can add a deep attribute: a deep traversal that listens for all properties in the object to change. Note that listening for array changes does not need to do this. * /
watch: {
// The first type monitors all attribute changes, directly monitors the entire attribute, consumption is not recommended
    obj: {
      handler(newValue, oldValue) {
        console.log("Object All Properties Listening", newValue, oldValue);
      },
      deep: true
    },
 
// The second type listens for a change in an object's property
"obj.name": {
       immediate: true.//true first load execution, false by default
      handler(newValue, oldValue) {
        console.log("Single attribute Listener", newValue, oldValue); }},} You can also set the computed property computed to listenomputed: {
  getName: function() {
    return this.obj.name
  }
}
watch: {
   getName: {
     handler: function() {
      / / to monitor}}},Copy the code

The watch function is not called when it is loaded for the first time. The watch function can be invoked only when the value changes. To invoke the watch function for the first time, use the immediate attribute. Simply use immediate: true in the corresponding function;