9951 explained code solutions for 126 technologies


vue.jsHow do I update data using Vue.js?


Updating data with Vue.js is relatively straightforward. You can use the Vue.set() method to update data in the Vue instance. The syntax for the Vue.set() method is as follows:

Vue.set(object, key, value)

Where object is the object that contains the data you want to update, key is the name of the property you want to update and value is the new value you want to assign to the property. For example:

let data = {
  name: 'John Doe'
};

Vue.set(data, 'name', 'Jane Doe');

console.log(data.name); // Output: Jane Doe

You can also use the this.$set() instance method to update data inside the Vue instance. The syntax for the this.$set() instance method is as follows:

this.$set(object, key, value)

Where object is the object that contains the data you want to update, key is the name of the property you want to update and value is the new value you want to assign to the property. For example:

let app = new Vue({
  data: {
    name: 'John Doe'
  }
});

app.$set(app.data, 'name', 'Jane Doe');

console.log(app.data.name); // Output: Jane Doe

You can also use the Vue.delete() method to delete properties from the Vue instance. The syntax for the Vue.delete() method is as follows:

Vue.delete(object, key)

Where object is the object that contains the data you want to delete and key is the name of the property you want to delete. For example:

let data = {
  name: 'John Doe'
};

Vue.delete(data, 'name');

console.log(data.name); // Output: undefined

Helpful links

Edit this code on GitHub