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:
|
<<: A brief discussion on ifnull() function similar to nvl() function in MySQL
>>: Detailed explanation of the installation commands and usage of Docker and FastDFS
Introduction to Debian Debian in a broad sense re...
In the process of designing a web page, designers...
For example: Copy code The code is as follows: <...
1. All tags must have a corresponding end tag Prev...
When encapsulating the date picker, you need to d...
Sometimes we want to implement such a function: c...
Table of contents 1. Memory model and runtime dat...
The use of ElementUI paging component Pagination ...
Why do we need to optimize SQL? Obviously, when w...
1. Stop the database server first service mysqld ...
Problem phenomenon: [root@localhost ~]# docker im...
As shown below: The test command determines wheth...
Table of contents Overview What is Image Compress...
Preface The reason for writing this article is mai...
Table of contents Preface Front-end structure Bac...