Detailed explanation of Vue's monitoring method case

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue

watch

Notice

Name: You should give the same name to the attribute you want to monitor.

1. Function

Used to monitor data changes in the vue instance and modify state changes at any time

2. Trigger conditions

When the property you monitor changes, the corresponding monitoring method will be called automatically

3. Usage scenarios

Used for asynchronous processing and relatively expensive operations

4. Examples

	 watch:{
             name(newvalue,oldvalue){
                  //Calculated properties can accept two parameters, the first parameter is the new property value, the second parameter is the old property value // this.age
                    // console.log('name property has changed')
                    console.log(newvalue,oldvalue)
                }

5. When monitoring an object

<script>
export default {
    data() {
        return {
            obj: {
                name: "Zhang San",
                age: 20,
                children: [
                    {
                        name: "Li Si",
                        age: 27
                    },
                    {
                        name: "Wang Wu",
                        age: 23
                    }
                ]
            }
        };
    },
    watch:
        obj: {
            handler: function(newVal, oldVal) {
                console.log("newVal:", newVal);
                console.log("oldVal:", oldVal);
            },
            deep: true,
            immediate: true
        },
        "obj.name": function(newVal, oldVal) {
            
            console.log("newVal obj.name:", newVal);
            console.log("oldVal obj.name:", oldVal);
        }
    },
};
</script>

When monitoring an object, you need to add deep:true so that you can monitor it in real time at the bottom layer. If you don’t add it, the object will not be able to monitor changes.

immediate attribute: Boolean value
immediate: true: monitor data changes when loading for the first time
immediate: false: listen only when changes occur

deep:true;

It turns on deep listening, that is, listeners are added to all attributes, and the handler function is executed if one of them changes.

This is the end of this article about the detailed case of Vue's monitoring method. For more relevant Vue's monitoring method content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief analysis of the use of watchEffect in Vue3
  • Usage and best practice guide for watch in Vue3
  • Summary of the use of vue Watch and Computed
  • How to understand the difference between computed and watch in Vue
  • Solution to the problem of repeated triggering of functions in Vue project watch
  • Reasons and solutions for multiple executions of the watch method when Vue monitors route changes
  • The use and difference between vue3 watch and watchEffect
  • Simple method example of vue watch monitoring object

<<:  MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

>>:  CentOS 6 uses Docker to deploy redis master-slave database operation example

Recommend

JavaScript to achieve text expansion and collapse effect

The implementation of expanding and collapsing li...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

Vue.js uses Element-ui to implement the navigation menu

This article shares the specific code for impleme...

Tutorial on installing jdk1.8 on ubuntu14.04

1. Download jdk download address我下載的是jdk-8u221-li...

A possible bug when MySQL executes the sum function on the window function

When using MySql's window function to collect...

Implementation of CSS circular hollowing (coupon background image)

This article mainly introduces CSS circular hollo...

Standard summary for analyzing the performance of a SQL statement

This article will introduce how to use explain to...

An article to help you thoroughly understand position calculation in js

Table of contents introduction scroll Element.scr...

Detailed explanation of Linux command unzip

Table of contents 1. unzip command 1.1 Syntax 1.2...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

MySQL 8.0.18 Installation Configuration Optimization Tutorial

Mysql installation, configuration, and optimizati...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...