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

Some parameter descriptions of text input boxes in web design

<br />In general guestbooks, forums and othe...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...

Detailed tutorial on installing Docker on CentOS 7.5

Introduction to Docker Docker is an open source c...

How to monitor array changes in JavaScript

Preface When introducing defineProperty before, I...

MySQL dual-master (master-master) architecture configuration solution

In enterprises, database high availability has al...

Detailed steps for creating a Vue scaffolding project

vue scaffolding -> vue.cli Quickly create a la...

Application scenarios and design methods of MySQL table and database sharding

Many friends have asked in forums and message are...

MySQL Error 1290 (HY000) Solution

I struggled with a problem for a long time and re...

Implementing search box function with search icon based on html css

Preface Let me share with you how to make a searc...

Summary of the use of Datetime and Timestamp in MySQL

Table of contents 1. How to represent the current...

Analyze how uniapp dynamically obtains the interface domain name

background The interface domain name is not hard-...

Summary of methods for finding and deleting duplicate data in MySQL tables

Sometimes we save a lot of duplicate data in the ...

Install MySQL 5.7.18 using rpm package under CentOS 7

I have been using MySQL recently. The article mys...