Use of this.$set in VueBackground : 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 Here is a small example: Add an age property to an object and make it change responsively <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 The data is indeed added, but the page does not render the age property responsively. When we use 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. analyzeThe 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. SummarizeThis 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:
|
<<: How to query the intersection of time periods in Mysql
>>: PNG Alpha Transparency in IE6 (Complete Collection)
The code looks like this: SELECT @i:=@i+1 rowNum,...
The worst option is to sort the results by time a...
Table of contents 1. Download 2. Install nginx an...
1. Installation Search the mariadb version to be ...
MySQL 5.7.18 free installation version installati...
The requirement is to pass in the rating data for...
System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...
Portainer is a lightweight docker environment man...
1. What are CSS methodologies? CSS methodologies ...
The official source code of monaco-editor-vue is ...
1. The mysqldump backup method uses logical backu...
I won't go into details about how important b...
Preface This article summarizes some common MySQL...
Create a Directory cd /usr/local/docker/ mkdir je...
<br />Simple example of adding and removing ...