Case study of dynamic data binding of this.$set in Vue

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the Internet is messy. Let me summarize its binding of single data, objects, arrays, and json data.

Without further ado, let’s get straight to the code:

<template>
  <div>
    <!-- Single data -->
    <button @click="text0a">text0</button>
    <p>text0: {{text0}}</p>
    <!-- Object -->
    <button @click="textObja">textObj</button>
    <p>textObj.text1: {{textObj.text1}}</p>
    <!-- Array -->
    <button @click="textArra">textArr</button>
    <p>textArr[1]: {{textArr[1]}}</p>
    <!-- json data -->
    <button @click="textJsona">textJson</button>
    <p>textJson[1].t5: {{textJson[1].t5}}</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      text0: '',
      textObj: {},
      textArr: [],
      textJson:[
        {t0: ''},
        {t4: ''},
        {t7: ''},
      ]
    }
  },
  methods: {
    text0a: function () {
      let vm = this
      let text100 = 'efghjk'
      vm.$set(vm,'text0',text100) 
      //Equivalent to vm.$set(vm,'text0','efghjk')
    },
    textObja: function () {
      let vm = this
      let textObj100 = {
        text1: '123',
        text2: '456'
        }
      vm.$set(vm.textObj,'text1',textObj100.text1) 
      //This is equivalent to vm.$set(vm,'textObj',textObj100)
    },
    textArra: function () {
      let vm = this
      let textArr200 = ['a1','a2','a3']
      vm.$set(vm,'textArr',textArr200)
    },
    textJsona: function () {
      let vm = this
      let textJson300 = [
        {t1: 't1',t2: 't2',t3: 't3'},
        {t4: 't4',t5: 't5',t6: 't6'},
        {t7: 't7',t8: 't8',t9: 't9'},
      ]
      vm.$set(vm.textJson[1],'t5',textJson300[1].t5) 
      //This is equivalent to vm.$set(vm,'textJson',textJson300)
    }
  }
}
</script>
<style>
</style>

Supplement: Vue uses $set to dynamically set attributes for data

In the actual development process, when binding a model to a form element, the attributes of the bound element are dynamically generated based on the background data. If you use the conventional assignment method, you cannot update the view

Need to use

this.$set(dataName,keyName,keyValue)
export default {
 data:{
  //First define an empty object formObject:{},
  arrayList:[],
 },
 mounted() {
  this.initPage()
 },
 methods:{
  initPage(){
   this.$store.dispatch(namespace/callData).then(res=>{
    // Set key-value for the data
    res.data.forEach(item=>{
     // ❗❗❗❗❗ this.formObject[item.name] = item.value // ❗❗❗❗ This method cannot update the view this.$set(this.formObject, item.name, item.value) // ✅✅✅✅Can update the view})
   })
   // Modify the array this.$store.dispatch(namespace/callData).then(res=>{
    // Set key-value for the data
    this.$set(this.arrayList,0,(res.data)[0].id) ✅✅✅✅Can update the view})
  }
 }
}

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Vue.set() and this.$set() usage and difference
  • 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
  • Detailed explanation of the use of this.$set in Vue

<<:  A brief discussion on ifnull() function similar to nvl() function in MySQL

>>:  Detailed explanation of the installation commands and usage of Docker and FastDFS

Recommend

Docker uses Supervisor to manage process operations

A Docker container starts a single process when i...

How to quickly build ELK based on Docker

[Abstract] This article quickly builds a complete...

Implementation code for infinite scrolling with n container elements

Scenario How to correctly render lists up to 1000...

MySQL MyISAM default storage engine implementation principle

By default, the MyISAM table will generate three ...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

JavaScript to achieve digital clock effect

This article example shares the specific code of ...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

What is Nginx load balancing and how to configure it

What is Load Balancing Load balancing is mainly a...

Markup Languages ​​- What to learn after learning HTML?

Click here to return to the 123WORDPRESS.COM HTML ...