How to use watch listeners in Vue2 and Vue3

How to use watch listeners in Vue2 and Vue3

watch : listen for data changes (change events of a certain value)

vue2.x

 data(){
     return {
         num:10
     }
 },
 watch:{
      num:{
	      /*
	       * newValue: current value * oldValue: modify the value of the last moment */
          handler(newValue,oldValue){
          	// doSomething
          },
          /*
           * deep: Boolean: Deep monitoring * true: monitor heap changes * false: only monitor stack changes (default)
           */
          deep:true/false,
          /*
           * immediate:Boolean: Whether to execute the handler function when it is first defined* true: execute the handler function when it is first defined* false: execute the handler function after modification*/          
          immediate:true/false
      }      
  }

vue3.x

Watch is used to monitor responsive data

Basic Use

const num = ref(0)
1. Import import {watch} from 'vue'
2. Use `const return value = watch(value to be monitored, (newVal,oldVal)=>{ }, {deep,immediate,flush})`
	 
	 Return value: You can turn off monitoring: return value()
	 Parameter 1: The value to be monitored Basic data types (Number, String, Boolean, null, undefined): () => Basic data type value Complex data types (Array, Object, Function): Directly write / () => Basic data type value Parameter 2: Analogy to the handler function in Vue2 Parameter 3: {} object, the object can have a configuration item: deep, immediate, flush,
	 		The meanings of deep and immediate have been described above. Here we mainly explain the values ​​of flush:
	 			 `flush:post/sync/pre
      				   pre (default value): before rendering, the value is changed and not rendered to the DOM
      				   post: After rendering, the value changes and is also rendered to the DOM
    				   sync: Render once per change, each time before rendering`
	 		

Note:
In actual development, no changes can be detected and unified use is used

watch(()=>responsive data,()=>{},{deep:true})

The above is the detailed content of how to use the watch listener in Vue2 and Vue3. For more information on the use of watch listeners, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the watch listener example in vue3.0
  • Solve the problem of undefined when calling this in vue listener watch
  • Vue 2.0 listener watch attribute code detailed explanation
  • Advanced examples of watch usage in Vue.js
  • An article teaches you how to use Vue's watch listener

<<:  MySql index improves query speed common methods code examples

>>:  Tomcat CentOS installation process diagram

Recommend

How to safely shut down MySQL

When shutting down the MySQL server, various prob...

18 Amazing Connections Between Interaction Design and Psychology

Designers need to understand psychology reading n...

Detailed explanation of Vue's seven value transfer methods

1. From father to son Define the props field in t...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...

js implements mouse in and out card switching content

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

Summary of HTML Hack Tags in IE Browser

Copy code The code is as follows: <!--[if !IE]...

Use ab tool to perform API stress test on the server

Table of contents 1 A brief introduction to syste...

JavaScript ES6 Module Detailed Explanation

Table of contents 0. What is Module 1.Module load...

Tips for using top command in Linux

First, let me introduce the meaning of some field...

MySQL slow query: Enable slow query

1. What is the use of slow query? It can record a...

TypeScript learning notes: type narrowing

Table of contents Preface Type Inference Truth va...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

Ubuntu MySQL version upgraded to 5.7

A few days ago, the library said that the server ...