SoFunction
Updated on 2025-04-10

Vue uses vm.$set to solve the problem that new attributes are not responding to

In, when you add a new property to an object that has been created, Vue cannot detect the change and automatically update the view. This is because Vue performs responsive conversion of data during initialization, and only attributes that exist in this process are converted into responsive attributes.

To solve this problem, Vue provides a special method vm.$set(object, propertyName, value), or the global (object, propertyName, value) method can also be used. This method is used to add new properties to the object and make sure that this new property is responsive, allowing Vue to detect changes and update views.

Here is a sample code to solve the problem that new attributes cannot respond using the vm.$set() method:

<template>
  <div>
    <p>Message: {{ message }}</p>
    <button @click="addNewProperty">Add New Property</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      message: 'Hello, Vue!'
    };
  },
  methods: {
    addNewProperty() {
      this.$set(this.$data, 'newProperty', 'New Value');
    }
  }
};
</script>

In this example, initially, the message property is responsive because it is defined in the data function. When the button is clicked, the addNewProperty method adds a new property named newProperty to this.$data using the vm.$set() method and gives it a new value. By using the vm.$set() method, Vue converts this new property into a responsive property, allowing Vue to detect this change and update the view.

It should be noted that the vm.$set() method can only be used to add new properties to objects that have been instantiated by Vue. If you need to dynamically add properties to an array of objects, you can use the push() method or the splice() method of the array, because Vue can detect calls to these array methods and update the view.

When you need to add new elements to the array and want Vue to detect changes and update the view, you can use the following method:

1. Use the() method to add a new element to the end of the array:

(newValue);

2. Use the() method to insert a new element into the specified position of the array:

(index, 0, newValue);

Both methods trigger Vue's responsive update mechanism, allowing Vue to detect changes in the array and update related views.

Here is a sample code that shows how to add new elements to an array and enable Vue to respond to updates:

<template>
  <div>
    <ul>
      <li v-for="item in myArray" :key="item">{{ item }}</li>
    </ul>
    <button @click="addItem">Add Item</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      myArray: ['Item 1', 'Item 2', 'Item 3']
    };
  },
  methods: {
    addItem() {
      ('New Item');
    }
  }
};
</script>

In this example, initially, there are three elements in the myArray array. When the "Add Item" button is clicked, the addItem method will use the push() method to add a new element to the end of the array. Thanks to the push() method, Vue can detect changes in the array and update the view, adding new elements to the list dynamically.

To sum up, by adding new elements to the array using the push() method or the splice() method, Vue can detect changes in the array and update the view. This allows you to dynamically add elements in the array and ensure that Vue can respond and update related views correctly.

This is the article about Vue using vm.$set() to solve the problem that new attributes of Vue cannot respond. For more related content related to Vue using vm.$set(), please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!