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

CSS isolation issue in Blazor

1. Environment VS 2019 16.9.0 Preview 1.0 .NET SD...

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration y...

Example code for implementing a simple search engine with MySQL

Table of contents Preface Introduction ngram full...

Bootstrap3.0 study notes table related

This article mainly explains tables, which are no...

Realize the CSS loading effect after clicking the button

Since there is a button in my company's produ...

Echarts Basic Introduction: General Configuration of Bar Chart and Line Chart

1Basic steps of echarts Four Steps 1 Find the DOM...

How to automatically start RabbitMq software when centos starts

1. Create a new rabbitmq in the /etc/init.d direc...

Discuss the value of Web standards from four aspects with a mind map

I have roughly listed some values ​​to stimulate ...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Using js to realize dynamic background

This article example shares the specific code of ...

Use Vue3 for data binding and display list data

Table of contents 1. Comparison with Vue2 1. New ...

WeChat applet date and time component (year, month, day, hour, and minute)

This article example shares the specific code of ...

How to create an Nginx server with Docker

Operating environment: MAC Docker version: Docker...