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
Table of contents Add traffic function to github+...
A Docker container starts a single process when i...
Original Intention The reason for making this too...
[Abstract] This article quickly builds a complete...
Scenario How to correctly render lists up to 1000...
By default, the MyISAM table will generate three ...
Code: Copy code The code is as follows: <!DOCTY...
This article example shares the specific code of ...
It took me three hours to install MySQL myself. E...
What is a table? It is composed of cell cells. In...
Preface Today, Prince will talk to you about the ...
Table of contents Preface 1. Routing lazy loading...
question The seamless scrolling of pictures and t...
What is Load Balancing Load balancing is mainly a...
Click here to return to the 123WORDPRESS.COM HTML ...