ListenerWhile computed properties are more appropriate in most cases, there are times when a custom listener is necessary. That’s why Vue provides a more general way to respond to changes in data through the watch option. This approach is most useful when you need to perform asynchronous or expensive operations when data changes. 1.watchEffectExecute immediately, no immediate Listening, watchEffect is executed immediately, there is no immediate, no need to pass the listening content, automatically perceives code dependencies, no need to pass parameters, only need to pass a callback function, cannot get the previous value If you need to disable the listener, you can call back this listener function const stop = watchEffect(()=>{ // console.log('num:',num.value); // console.log('num:',str.value); }) 2.watchIt will not be executed immediately, you need to manually start the immediate //Specify the value num to listen to watch(num,(val,oval)=>{ // val: new value, oval: previous value // console.log(num.value); // console.log(val,oval); },{//The second parameter obj immediate,deep immediate:true//By default, it will monitor only when the data changes. // It will not be executed when it is created for the first time. Set it to true and it will be executed for the first time.
1.1 The first way to listen//Listen to the changes of id and object data under statewatch(state,(val,oval)=>{ // console.log('id',val.id,oval); },{ immediate:true, deep:true//Turn on deep monitoring to detect changes in object attribute values}) 1.2 The second way to listen// Listen to state.uname watch(()=>state.uname,(uname,p)=>{ // new value of uname, old value of p console.log(uname,p); },{ immediate:true }) 1.3 Listening to multiple data sources//Listen for multiple data (id, uname) //()=>state.id, equivalent to //object.values(toRefs(state)) deconstructs const stop = watch([()=>state.id,()=>state.uname],([id,uname],[oid,oname])=>{ // id new, oid old console.log('id',id,oid); // uname new, oname old console.log('uname',uname,oname); }) SummarizeThis is the end of this article on various listening methods of Vue3.0. For more relevant content on Vue3.0 listening methods, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation on how to install MySQL database on Alibaba Cloud Server
>>: Call and execute host docker operations in docker container
Question: Is the origin server unable to find a r...
Although Microsoft has done a lot of research and ...
Project requirements: When you click a product tr...
This article introduces blue-green deployment and...
Table of contents 1. Introduction 2. Introduction...
Generate SSL Key and CSR file using OpenSSL To co...
Table of contents 1. unzip command 1.1 Syntax 1.2...
In order to provide high availability of the netw...
Must read before operation: Note: If you want to ...
Today, when I was writing a small program, I used...
I encountered a pitfall when writing dynamic form...
Table of contents background Solution 1 Ideas: Co...
Table of contents Overview Implementation Protect...
First, let me explain that what we want to do is ...
Similar structures: Copy code The code is as foll...