Summary of Vue watch monitoring methods

Summary of Vue watch monitoring methods

In vue , watch is used to respond to data changes. There are roughly three ways to use watch .

1. The role of watch in vue is just like its name, which is the role of monitoring

For example, there is an object:

watchData: {
    name: '',
    age: '',
}


2. Monitor the properties of this object

watchData: {
    handler: function() {
        console.log();
    },
    deep: true
}


The monitoring object can be monitored with deep , otherwise the changes of the object cannot be detected

3. Monitor the properties of this object

Method 1:

watch:
 'watchData.name'(newValue, oldValue) {
     console.log(newValue);
 }
}


Method 2:

watch:
    'watchData.name': {
        handler: function() {
            console.log();
        }
    }
},


Why do we need to monitor the properties of an object? If we monitor an object, any data inside the object will cause watch to be re-executed. This may not be what you want, that is, to monitor a certain property change before executing watch , or to monitor any property change inside the object before executing watch , which will also cause a certain loss in performance. Therefore, we use this method of monitoring a single property of the object to deal with it, and wrap the "object.property to be monitored in quotation marks"

4. Monitor the properties of this object

computed: {
    getName() {
        return this.watchData.name 
    }
},
watch:
    getName(newValue, oldValue) {
        console.log(newValue);
    }
},


This method is actually the same as the second one. The difference is that computed is used, and then the getName method is listened to. In fact, the getName method returns name attribute in the watchData object.

This is the end of this article about the summary of vue watch monitoring method. For more relevant vue watch 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:
  • Solve the pitfalls of calling methods in Vue watch
  • Detailed explanation of watch monitoring data changes in vue and each attribute in watch
  • Detailed explanation of the basic usage of VUE watch listener
  • Triggering timing of watch listener in vue (pitfalls of watch and solutions)

<<:  DHCP Configuration Tutorial in CentOS7 Environment

>>:  CSS implements 0.5px lines to solve mobile compatibility issues (recommended)

Recommend

React's reconciliation algorithm Diffing algorithm strategy detailed explanation

Table of contents Algorithmic Strategy Single-nod...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

How Database SQL SELECT Queries Work

As Web developers, although we are not profession...

Linux uses join -a1 to merge two files

To merge the following two files, merge them toge...

js development plug-in to achieve tab effect

This article example shares the specific code of ...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

Sample code of uniapp vue and nvue carousel components

The vue part is as follows: <template> <...

Use personalized search engines to find the personalized information you need

Many people now live on the Internet, and searchin...

Solution to the problem that the image name is none after Docker load

Recently, I found that after using the docker loa...

JavaScript to achieve a simple message board case

Use Javascript to implement a message board examp...

How to use the Marquee tag in XHTML code

In the forum, I saw netizen jeanjean20 mentioned h...

Front-end development must learn to understand HTML tags every day (1)

2.1 Semanticization makes your web pages better u...

Several ways to implement image adaptive container with CSS (summary)

There is often a scenario where the image needs t...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...