Vue monitor propertiesWhat is the listener property?The so-called monitoring is to monitor the status or property changes of built-in objects and respond to them. Monitoring properties means that you can monitor changes in other data. What is the difference between listening properties and calculating properties?Computed properties are cached and the results are recalculated and the DOM is updated after the dependent value changes. The property monitors the property value, and when the defined value changes, the corresponding function is executed. The main difference in usage: Computed properties cannot perform asynchronous tasks. Computed properties are generally not used to request servers or perform asynchronous tasks, because this may take a long time, and our calculated properties need to be updated in real time. So this asynchronous task can be done using the listening property. In a word: What computed can achieve, watch can achieve, and what computed cannot achieve, watch can also achieve Use of monitoring propertiesUse the watch configuration item and write the property to be monitored in the configuration item. Each change in the property value will trigger the handler function callback. You can also monitor changes in calculated properties. example: Monitor changes in the input box Code <template> <div class="main"> Me: <el-input v-model="name" placeholder="Please enter your name"></el-input> Friend 1<el-input v-model="friends.friend_1" placeholder="Please enter your name"></el-input> Friend 2<el-input v-model="friends.friend_2" placeholder="Please enter your name"></el-input> </div> </template> <script> export default { data(){ return { name:'Romantic Coder', friends:{friend_1:'张三',friend_2:'李四'} } }, watch:{ name:{ handler(newValue,oldValue){ //newValue new value, oldValue value before change console.log(newValue,oldValue) this.friends.friend_1='Wang Wu' } }, //Monitor the change of a certain attribute in the multi-level structure 'friends.friend_1':{ handler(newValue,oldValue){ console.log(newValue,oldValue) } }, 'friends.friend_2':{ handler(newValue,oldValue){ console.log(newValue,oldValue) } }, } }; </script> Deep MonitoringWhen our object has a multi-layer structure, we need to monitor many properties of the object, and avoid writing each property separately. At this time, deep monitoring is used. In the watch configuration, in the monitoring property object, set deep to true to monitor changes in values within multi-level objects or arrays. watch:{ //Monitor the changes of all attributes in the multi-level structure friends:{ handler(newValue,oldValue){ console.log(newValue,oldValue,"aa") }, deep:true, //enable deep monitoring} } Note : There is a problem here. When deep monitoring occurs, the new and old values will be the same? Official explanation: When mutating (not replacing) an object or array, the old value will be identical to the new value, since their references point to the same object/array. Vue does not keep a copy of the value before mutation This causes the pointer to change. In my article "Do You Know How to Deep Copy JS?", I talked about data storage. Call nowSame as deep configuration location. watch:{ name:{ handler(newValue,oldValue){ //newValue new value, oldValue value before change console.log(newValue,oldValue) this.friends.friend_1='Wang Wu' }, immediate:true }, } It is executed once when the page is loaded, so the old data is SummarizeThe watch listening property can usually be used for data persistence, event dispatching and synchronous/asynchronous execution, format verification... This 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:
|
<<: Three ways to achieve background blur in CSS3 (summary)
>>: Problems and solutions for deploying Nginx+KeepAlived cluster dual-active architecture in VMware
What are HTTP Headers HTTP is an abbreviation of ...
Dynamic rem 1. First, let’s introduce the current...
Use vite to build a vue3 project You can quickly ...
Click here to return to the 123WORDPRESS.COM HTML ...
For Centos installation of MySQL, please refer to...
1. Check whether event is enabled show variables ...
First download the compressed version of mysql, t...
Sometimes it’s nice to see some nice scroll bar e...
Table of contents Character Set Comparison Rules ...
1. COUNT(*) and COUNT(COL) COUNT(*) usually perfo...
Nginx supports three ways to configure virtual ho...
If you have just come into contact with Linux, th...
Preface In current JavaScript, there is no concep...
HTML Design Pattern Study Notes This week I mainl...
Xiaobai records the installation of vmtools: 1. S...