Detailed explanation of the use of this.$set in Vue

Detailed explanation of the use of this.$set in Vue

Use of this.$set in Vue

Background : When I was writing a front-end project, the back-end gave us a json object, and I rendered it on the page. However, due to my own needs, I want to add a field to the returned object, so I push a field into it. The field is added, but the page rendering does not change. Later I realized it was not responsive. If we want this new field to be responsive, we need to use this.$set to inject data.

When an object or array (the value in the array is an object) is declared or assigned in Vue's data, adding a new property to the object and updating the value of this property will not update the view.

use

this.$set( target, key, value)

target : represents the data source, which is the array or object you want to operate on

key : the field to be operated

value : the data to be changed

Here is a small example:

Add an age property to an object and make it change responsively

insert image description here

<template>
  <div class="text">
      <p>{{list}}</p>
      <button @click="add">age++</button>
  </div>
</template>
<script>
export default {
    data(){
        return {
            list:{ name: "Zhang San"}
        }
    },
    methods: {
        add(){
            if(!this.list.age){ // If there is no age attribute, add an age attribute to it this.list.age=18
            }else{
                this.list.age++
            }
            console.log(this.list)
        }
    }
}
</script>

When we do not use this.$set to add object properties, the effect is:

The data is indeed added, but the page does not render the age property responsively.

insert image description here

When we use this.$set(this.list,'age',18) to add an attribute. Effect:

insert image description here

We can see that the added data is responsive.

Why responsive?

If you look closely, you can see that this.$set has additional get age and set age methods, which is why it is responsive.

insert image description here

analyze

The responsive principle of Vue is that a JavaScript object is passed into a Vue instance as a data option. Vue will traverse all the properties of this object and use Object.defineProperty to convert all these properties into getters/setters. These getters/setters are invisible to the user, but internally they allow Vue to track dependencies and notify changes when properties are accessed and modified. It should be noted here that different browsers format getter/setter differently when printing data objects in the console. The following figure is from the official documentation.

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue.set() and this.$set() usage and difference
  • Case study of dynamic data binding of this.$set in Vue
  • Solution to Vue error TypeError: this.$set is not a function
  • Vue.set() this.$set() triggers view updates and considerations
  • Parsing Vue.set() and this.$set() from vue source code

<<:  How to query the intersection of time periods in Mysql

>>:  PNG Alpha Transparency in IE6 (Complete Collection)

Recommend

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Mysql query the most recent record of the sql statement (optimization)

The worst option is to sort the results by time a...

Analyze the method of prometheus+grafana monitoring nginx

Table of contents 1. Download 2. Install nginx an...

Analysis of the configuration process of installing mariadb based on docker

1. Installation Search the mariadb version to be ...

MySQL 5.7.18 free installation version configuration tutorial

MySQL 5.7.18 free installation version installati...

Implementation of React star rating component

The requirement is to pass in the rating data for...

Install and configure MySQL under Linux

System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...

Tutorial on using portainer to connect to remote docker

Portainer is a lightweight docker environment man...

An example of using CSS methodologies to achieve modularity

1. What are CSS methodologies? CSS methodologies ...

Vue uses the method in the reference library with source code

The official source code of monaco-editor-vue is ...

Summary of Binlog usage of MySQL database (must read)

I won't go into details about how important b...

An article to master MySQL index query optimization skills

Preface This article summarizes some common MySQL...

Practical notes on installing Jenkins with docker-compose

Create a Directory cd /usr/local/docker/ mkdir je...

Simple example of adding and removing HTML nodes

<br />Simple example of adding and removing ...