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
More of Vue.js
- How do I download a zip file using Vue.js?
- How to use a YAML editor with Vue.js?
- How do I use the v-if directive in Vue.js?
- How do I use the v-model in Vue.js?
- How do I set up unit testing for a Vue.js application?
- How do I unmount a Vue.js component?
- How can I convert XML data to JSON using Vue.js?
- How do I use Vue.js lifecycle hooks?
- How do I use a SVG logo with Vue.js?
- How do I use a keypress event in Vue.js?
See more codes...