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

A brief talk about Mysql index and redis jump table

summary During the interview, when discussing abo...

Complete steps to use samba to share folders in CentOS 7

Preface Samba is a free software that implements ...

A link refresh page and js refresh page usage examples

1. How to use the link: Copy code The code is as f...

How to solve the problem of blurry small icons on mobile devices

Preface Previously, I talked about the problem of...

Introduction and installation of MySQL Shell

Table of contents 01 ReplicaSet Architecture 02 I...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

Analyzing Linux high-performance network IO and Reactor model

Table of contents 1. Introduction to basic concep...

Detailed explanation of the execution principle of MySQL kill command

Table of contents Kill instruction execution prin...

Quickly solve the problem that CentOS cannot access the Internet in VMware

Yesterday I installed CentOS7 under VMware. I wan...

Detailed explanation of mysql exists and not exists examples

Detailed explanation of mysql exists and not exis...

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you ...

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...