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
This article example shares the specific code of ...
Application of HTML and CSS in Flash: I accidental...
Recently, the server has been frequently cracked ...
Load balancing is a commonly used device in serve...
Today, when I was writing a small program, I used...
1. Introduction MySQL locks can be divided into g...
<br />Looking at this title, you may find it...
uniapp code <template> <view> <ima...
mysql storage engine: The MySQL server adopts a m...
After the release of CentOS8.0-1905, we tried to ...
Mysql5.5 dual machine hot standby Implementation ...
I developed a project some time ago. I used the f...
Table of contents infer Case: Deepen your underst...
Table of contents Uncontrolled components Control...
1. Links Hypertext links are very important in HTM...