1. New usage of watchIn the optional API, watch uses watch:{ mood(curVal,preVal){ console.log('cur',curVal);//Latest value console.log('pre',preVal);//Change the previous value} } 1.1.watch usage syntax In The syntax is: import { watch } from "vue" watch( name , ( curVal , preVal )=>{ //Business processing}, options ) There are three parameters:
It will not be executed when the page is first entered. When the value changes, the current latest value and the value before modification will be printed out. Example 1 : Listening for a data import { ref , watch } from "vue" export default{ setup(){ const mood = ref("") //Frame listener watch(mood,(curVal,preVal)=>{ console.log('cur',curVal); console.log('pre',preVal); },{ //Configuration items}) return { mood } } } 1.2. watch monitors multiple attribute valuesExample 2 : Listening to multiple properties watch([mood,target],([curMood,curTarget],[preMood,preTarget])=>{ console.log('curMood',curMood); console.log('preMood',preMood); console.log('curTarget',curTarget); console.log('preTarget',preTarget); },{ //Configuration items}) 1.3. watch monitor reference data type When The usage syntax is as follows: watch(()=>obj.name,(curValue,preValue)=>{ //Frame listens to a property of the reference data type},{ //Configuration items}) The first parameter, the callback function returns the properties of the object that needs to be listened to. The following parameters are the same as above. Example 3 : Frame listen object attribute <template> <div> {{obj}} <input type="text" v-model="obj.name"> </div> </template> <script> import { ref , reactive , watch } from "vue" export default{ setup(){ const obj = reactive({ name:'qq',sex:'女' }) watch(()=>obj.name,(cur,pre)=>{ console.log('cur',cur); },{ }) return { obj } } } </script> If we try to remove the attribute and directly monitor the entire object, we find 2.watchEffect Example 4 : Listening Object <template> <div> {{obj}} <input type="text" v-model="obj.name"> <input type="text" v-model="obj.sex"> </div> </template> <script> import { reactive , watchEffect } from "vue" export default{ setup(){ let obj = reactive({ name:'qq',sex:'女'}) watchEffect(() => { console.log('name',obj.name); console.log('sex' , obj.sex); }) return { obj } } } </script> The 3. The difference and connection between watch and watchEffect 3.1. Features of watch The features of watch are:
3.2.watch configuration itemsThe configuration items of watch can supplement the deficiencies of watch features. The configuration items are:
3.3. Features of watchEffectThe watchEffect side effect function has the following characteristics:
3.4. Relationship between watch and watchEffect The first two features of Example 5 : <template> <div> {{obj}} <input type="text" v-model="obj.name"> </div> </template> <script> import { ref , reactive , watch } from "vue" export default{ setup(){ const obj = reactive({ name:'qq',sex:'女' }) watch(()=>obj,(cur,pre)=>{ console.log('cur',cur); },{ immediate:true, deep:true }) return { obj } } } </script> This is the end of this article about the new usage of watch and watchEffect in vue 3. For more relevant content about watch and watchEffect in vue 3, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to distribute two buttons on the left and right sides of the same parent tag using CSS
>>: The difference between the knowledge of front-end developers and artists in website development
mysql accidentally deleted data Using the delete ...
Batch comments in SQL Server Batch Annotation Ctr...
Table of contents 1. Prerequisites 1.1 Supported ...
content Use scaffolding to quickly build a node p...
Author | Editor Awen | Produced by Tu Min | CSDN ...
Table of contents 1. Background running jobs 2. U...
Table of contents 1. Mysql data structure 2. The ...
Table of contents 01 Common Faults 1 02 Common Fa...
When using MySql's window function to collect...
Copy code The code is as follows: <hr style=&q...
I searched online and found that many interviews ...
The JD carousel was implemented using pure HTML a...
Preface Recently, due to work needs, I need to in...
1. Optimization of commonly used HTML tags HTML s...
When I created a Docker container today, I accide...