Comprehensive summary of Vue3.0's various listening methods

Comprehensive summary of Vue3.0's various listening methods

Listener

While 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.watchEffect

Execute 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.watch

It 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.
  • Listen to the data source of ref
  • Listening to reactive data sources

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);
      })

Summarize

This 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 of the watch listener example in vue3.0
  • Let's talk briefly about the changes in setup in vue3.0 sfc
  • Detailed use of Echarts in vue2 vue3
  • A brief discussion on several advantages of Vue3

<<:  Detailed explanation on how to install MySQL database on Alibaba Cloud Server

>>:  Call and execute host docker operations in docker container

Recommend

jQuery plugin to achieve image comparison

This article example shares the specific code of ...

Application of HTML and CSS in Flash

Application of HTML and CSS in Flash: I accidental...

CentOS server security configuration strategy

Recently, the server has been frequently cracked ...

Solve the scroll-view line break problem of WeChat applet

Today, when I was writing a small program, I used...

What you need to understand about MySQL locks

1. Introduction MySQL locks can be divided into g...

Design Tips: We think you will like it

<br />Looking at this title, you may find it...

Uniapp WeChat applet: Solution to key failure

uniapp code <template> <view> <ima...

How to modify the default storage engine in MySQL

mysql storage engine: The MySQL server adopts a m...

How to install and configure ftp server in CentOS8.0

After the release of CentOS8.0-1905, we tried to ...

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

The basic use of html includes links, style sheets, span and div, etc.

1. Links Hypertext links are very important in HTM...