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

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

Common commands for mysql authorization, startup, and service startup

1. Four startup methods: 1.mysqld Start mysql ser...

How to use Tencent slider verification code in Vue3+Vue-cli4 project

Introduction: Compared with traditional image ver...

Detailed installation process of mysql5.7.21 under win10

This article shares the installation of MySQL 5.7...

Sample code for implementing neon button animation effects with CSS3.0

Today I will share with you a neon button animati...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

How to print highlighted code in nodejs console

Preface When the code runs and an error occurs, w...

What I learned while building my own blog

<br />In one year of blogging, I have person...

Centos 7 64-bit desktop version installation graphic tutorial

If you think the system is slow and want to chang...

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encou...

MySQL 5.7.23 installation and configuration method graphic tutorial

This article records the installation tutorial of...

The basic principles and detailed usage of viewport

1. Overview of viewport Mobile browsers usually r...

Windows Server 2019 Install (Graphical Tutorial)

Windows Server 2019 is the latest server operatin...