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

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

Introduction to the use of this in HTML tags

For example: Copy code The code is as follows: <...

Summary of Seven Basic XHTML Coding Rules

1. All tags must have a corresponding end tag Prev...

Detailed explanation of the JVM series memory model

Table of contents 1. Memory model and runtime dat...

How to use ElementUI pagination component Pagination in Vue

The use of ElementUI paging component Pagination ...

Summary of B-tree index knowledge points in MySQL optimization

Why do we need to optimize SQL? Obviously, when w...

How to install mysql6 initialization installation password under centos7

1. Stop the database server first service mysqld ...

Solve the problem of docker's tls (ssl) certificate expiration

Problem phenomenon: [root@localhost ~]# docker im...

How to view the status of remote server files in Linux

As shown below: The test command determines wheth...

How to optimize images to improve website performance

Table of contents Overview What is Image Compress...

How to manage large file uploads and breakpoint resume based on js

Table of contents Preface Front-end structure Bac...