The use and difference between vue3 watch and watchEffect

The use and difference between vue3 watch and watchEffect

1.watch listener

Introducing watch

import { ref, reactive, watch, toRefs } from 'vue'

Monitor basic data types-----watch features:

1. It has a certain degree of laziness. It will not be executed when the page is displayed for the first time, but will be executed only when the data changes.

2. Parameters can get current values ​​and original values

3. You can listen to changes in multiple data and use one listener to carry them.

setup() {
	const name = ref('leilei')
	watch(name, (curVal, prevVal) => {
 	console.log(curVal, prevVal)
 })
}
template: `Name: <input v-model="name" />`

Listening to reference types-----

setup() {
	const nameObj = reactive({name: 'leilei', englishName: 'bob'})
 Listen to a data watch(() => nameObj.name, (curVal, prevVal) => {
 	console.log(curVal, prevVal)
 })
 Monitor multiple data watch([() => nameObj.name, () => nameObj.name], ([curName, curEng], [prevName, curEng]) => {
 	console.log(curName, curEng, '----', prevName, curEng)
  setTimeout(() => {
   stop1()
  }, 5000)
 })
 const { name, englishName } = toRefs(nameObj)
}
template: `Name: <input v-model="name" /> englishName: <input v-model="englishName" />`

2.watchEffect

No excessive parameters, only one callback function

1. Execute immediately, without laziness, and it will be executed when the page is first loaded.

2. Automatically detect internal code and execute it if there is a dependency in the code

3. No need to pass the content to be listened to. It will automatically sense the code dependency. No need to pass many parameters. Just pass a callback function

4. You cannot get the value of the previous data, you can only get the current value

5. Some = asynchronous operations would be more appropriate here

watchEffect(() => {
	console.log(nameObj.name) 
})

The usage of canceling a listener is the same as that of canceling a watch listener.

const stop = watchEffect(() => {
	console.log(nameObj.name) 
 setTimeout(() => {
 	stop()
 }, 5000)
})

const stop1 = watch([() => nameObj.name, () => nameObj.name], ([curName, curEng], [prevName, curEng]) => {
 	console.log(curName, curEng, '----', prevName, curEng)
  setTimeout(() => {
   stop1()
  }, 5000)
 })

Watch can also be changed to non-lazy immediate execution by adding a third parameter immediate: true

 watch([() => nameObj.name, () => nameObj.name], ([curName, curEng], [prevName, curEng]) => {
 	console.log(curName, curEng, '----', prevName, curEng)
  setTimeout(() => {
   stop1()
  }, 5000)
 }, {
 	immediate: true
 })

The above is the detailed content about the usage of vue3 watch and watchEffect and their differences. For more information about vue3 watch and watchEffect, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the usage of watch and watchEffect in VUE3
  • A brief analysis of the use of watchEffect in Vue3
  • Do you know about watch and watchEffect of vue3?
  • A brief discussion on the specific usage of watchEffect in Vue3
  • Detailed examples of watch and watchEffect in vue3

<<:  Solve the problem of docker pull being reset

>>:  Detailed explanation of the difference between Oracle10 partitions and MySQL partitions

Recommend

Summary of several replication methods for MySQL master-slave replication

Asynchronous replication MySQL replication is asy...

MySQL starts slow SQL and analyzes the causes

Step 1. Enable MySQL slow query Method 1: Modify ...

Summary of common knowledge points required for MySQL

Table of contents Primary key constraint Unique p...

HTML basic structure_Powernode Java Academy

Many times when learning web page development, th...

CSS to achieve the effect of rotating flip card animation

The css animation of the rotating flip effect, th...

How to install MySQL Community Server 5.6.39

This article records the detailed tutorial of MyS...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...

js to achieve simple drag effect

This article shares the specific code of js to ac...

In IIS 7.5, HTML supports the include function like SHTML (add module mapping)

When I first started, I found a lot of errors. In...

Installation and configuration method of vue-route routing management

introduce Vue Router is the official routing mana...

Solution to no Chinese input method in Ubuntu

There is no solution for Chinese input method und...

Introduction to ApplicationHost.config (IIS storage configuration area file)

For a newly created website, take ASP.NET MVC5 as...

Example of implementing text wrapping in html (mixed text and images in html)

1. Text around the image If we use the normal one...