JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Dynamically add properties and values ​​to objects in Vue

Author:JIYIK Last Updated:2025/02/28 Views:

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.setmethod for dynamically adding properties and values ​​to objects. Vue.setThe 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.setthe 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.setthe method, Vue also provides a setmethod, which is similar in usage. setThe 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 mountedthe hook function, we used this.$setthe method to dynamically add an attribute score to the obj object with a value of 90.


Direct assignment

In addition to using Vue.setthe and $setmethods, 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.setor $setmethod.


Note:

  1. 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.
  2. 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.
  3. 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.
  4. 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.setor $setmethod 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.

Article URL:

Related Articles

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

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.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial