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

Summary of MySQL lock related knowledge

Locks in MySQL Locks are a means to resolve resou...

Comparing Document Locations

<br />A great blog post by PPK two years ago...

Some key points of website visual design

From handicraft design to graphic design to web de...

MySQL scheduled task implementation and usage examples

This article uses examples to illustrate the impl...

Various ways to achieve the hollowing effect of CSS3 mask layer

This article introduces 4 methods to achieve mask...

js realizes the magnifying glass function of shopping website

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

How to get the maximum or minimum value of a row in sql

Original data and target data Implement SQL state...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

iview implements dynamic form and custom verification time period overlap

Dynamically adding form items iview's dynamic...

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...

How to use CSS styles and selectors

Three ways to use CSS in HTML: 1. Inline style: s...