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
Usage of alter command in mysql to edit table str...
Table of contents 1. Introduction 2. Simple defin...
This post focuses on a super secret Flutter proje...
1. Single row overflow 1. If a single line overfl...
This article introduces how to install the system...
Database read-write separation is an essential an...
The role of virtual DOM First of all, we need to ...
MySQL is a powerful open source database. With th...
I saw in the LOFTER competition that it was mentio...
The effect of this function is similar to vue的pro...
Preface We may have heard of the concept of rowid...
Effect display The built-in boot interface is too...
Table of contents Start by clicking the input box...
1. Alibaba Cloud selects the appropriate cloud se...
Table of contents How to operate Operation proces...