Dynamically add properties and values to objects in Vue
In Vue, we often need to manipulate the properties and values of objects. Sometimes we need to dynamically add properties and values to meet business needs. This article will introduce in detail how to dynamically add properties and values to objects in Vue, and provide some precautions.
Using Vue.set Method
Vue provides a Vue.set
method for dynamically adding properties and values to objects. Vue.set
The method receives three parameters, the first parameter is the object, the second parameter is the property name, and the third parameter is the property value.
For example, we have an object:
let obj = {
name: 'Tom',
age: 20
}
Now we need to dynamically add a property score to this object with a value of 90. We can use Vue.set
the method:
Vue.set(obj, 'score', 90)
At this point, the obj object becomes:
{
name: 'Tom',
age: 20,
score: 90
}
Using the $set Method
In addition to Vue.set
the method, Vue also provides a set
method, which is similar in usage. set
The method is a method of the Vue instance and can be used directly in the Vue component.
For example, we have a Vue component:
<template>
<div>{{ obj }}</div>
</template>
<script>
export default {
data() {
return {
obj: {
name: 'Tom',
age: 20
}
}
},
mounted() {
this.$set(this.obj, 'score', 90)
}
}
</script>
In mounted
the hook function, we used this.$set
the method to dynamically add an attribute score to the obj object with a value of 90.
Direct assignment
In addition to using Vue.set
the and $set
methods, we can also dynamically add properties and values to objects by directly assigning them. For example:
let obj = {
name: 'Tom',
age: 20
}
obj.score = 90
At this point, the obj object becomes:
{
name: 'Tom',
age: 20,
score: 90
}
However, it should be noted that direct assignment cannot trigger Vue's responsive system. If we need to use dynamically added properties in the template, it is recommended to use the
Vue.set
or$set
method.
Note:
- When dynamically adding properties and values to an object, it is recommended to use the Vue.set or $set method, which triggers Vue's responsive system and ensures that the template can correctly use the dynamically added properties.
- If you need to dynamically add properties and values to multi-layer nested objects, it is recommended to use the Vue.set or $set method to ensure that objects at all levels can correctly trigger responsiveness.
- The dynamically added attributes and values should not have the same names as the existing attributes and values, otherwise they will overwrite the existing attributes and values.
- When using the $set method in a Vue component, you need to pay attention to the this pointing problem. You can use arrow functions or bind methods to solve this problem.
Summarize:
Dynamically adding properties and values is one of the common operations in Vue development. In actual development, we need to choose the appropriate method to implement it according to specific needs. Using the Vue.set
or $set
method can ensure that Vue's responsive system can work correctly, so it is recommended to give priority to it.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:199 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during
How to scroll to the top or bottom of the page in Vue.js
Publish Date:2025/03/01 Views:96 Category:Vue
-
Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this
Display element or text on mouse hover in vue
Publish Date:2025/03/01 Views:179 Category:Vue
-
Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following
When watching an object in Vue, how to exclude certain properties from being watc
Publish Date:2025/03/01 Views:125 Category:Vue
-
When using watch in Vue, you may need to monitor an object, but only care about certain properties in the object, not all properties of the object. In this case, you can use deep monitoring and calculated properties, or add some options to w
What is the use of the immediate property of watch in Vue?
Publish Date:2025/03/01 Views:67 Category:Vue
-
In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang
Setting up checkbox functionality in Vue
Publish Date:2025/03/01 Views:186 Category:Vue
-
In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.
What happens if a child component changes the data in props in Vue?
Publish Date:2025/03/01 Views:140 Category:Vue
-
In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t
How to refresh the page in Vue
Publish Date:2025/03/01 Views:91 Category:Vue
-
Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the