How does Vue monitor an object and execute a method when the content of the object changes?

how does Vue monitor an object and execute a method when the content of the object changes?

Mar.03,2021

you can use the watch method in vue to listen to the object. Set deep:true to do deep listening. When the properties in the object change, the handler method will be called, and you can write your logic in it

.
<template>
  <div>
    <input v-model="user.name">
  </div>
</template>

<script>
export default {
  data () {
    return {
      user: {
          name: ''
      }
    }
  },

  watch:{
      user: {
        deep: true,
        handler: function (newVal,oldVal){
          console.log('newValue', newVal);
          console.log('oldValue', oldVal.name);
        }
      }
  },
}

it is recommended that you use watch listening events to complete


., watch: {'params.limit': function (newValue, oldValue) {}, name: function (newValue, oldValue) {}},

suppose you have an object in your ViewModel that is {params: {limit:'}, name:''}


solve this how to monitor?

<table ...>
  ...
  <tbody>
    <tr v-for="(item, index) in items" :key="index" :class="{ actived : selected === index}">
      <td>{{ item.name }}</td>
      <td>
        <input v-if="item.editable" v-model="item.age" type="number" ... />
        <span v-else>{{ item.age }}</span>
      </td>
    </tr>
  </tbody>
</table>

question:
how do I automatically focus on the input box when a row is selected for editing? < td: ref= "'input' + index". > invalid , < tr index for = "(item, index) in items": key= "index": ref= "item". > is also not available through $el.querySelector (' input.number') .

Menu