Detailed explanation of Vue's monitoring properties

Detailed explanation of Vue's monitoring properties

Vue monitor properties

What is the listener property?

The so-called monitoring is to monitor the status or property changes of built-in objects and respond to them. Monitoring properties means that you can monitor changes in other data.

What is the difference between listening properties and calculating properties?

Computed properties are cached and the results are recalculated and the DOM is updated after the dependent value changes.

The property monitors the property value, and when the defined value changes, the corresponding function is executed.

The main difference in usage:

Computed properties cannot perform asynchronous tasks. Computed properties are generally not used to request servers or perform asynchronous tasks, because this may take a long time, and our calculated properties need to be updated in real time. So this asynchronous task can be done using the listening property.

In a word: What computed can achieve, watch can achieve, and what computed cannot achieve, watch can also achieve

Use of monitoring properties

Use the watch configuration item and write the property to be monitored in the configuration item. Each change in the property value will trigger the handler function callback. You can also monitor changes in calculated properties.

insert image description here

example:

Monitor changes in the input box

insert image description here

Code

<template>
<div class="main">
    Me: <el-input v-model="name" placeholder="Please enter your name"></el-input>
    Friend 1<el-input v-model="friends.friend_1" placeholder="Please enter your name"></el-input>
    Friend 2<el-input v-model="friends.friend_2" placeholder="Please enter your name"></el-input>
</div>
</template>
<script>
export default {
  data(){
    return {
      name:'Romantic Coder',
      friends:{friend_1:'张三',friend_2:'李四'}
    }
  },
  watch:{
    name:{
      handler(newValue,oldValue){ //newValue new value, oldValue value before change console.log(newValue,oldValue)
        this.friends.friend_1='Wang Wu'
      }
    },
    //Monitor the change of a certain attribute in the multi-level structure 'friends.friend_1':{
      handler(newValue,oldValue){
        console.log(newValue,oldValue)
      }
    },
    'friends.friend_2':{
      handler(newValue,oldValue){
        console.log(newValue,oldValue)
      }
    },
  }
};
</script>

Deep Monitoring

When our object has a multi-layer structure, we need to monitor many properties of the object, and avoid writing each property separately. At this time, deep monitoring is used.

In the watch configuration, in the monitoring property object, set deep to true to monitor changes in values ​​within multi-level objects or arrays.

 watch:{
    //Monitor the changes of all attributes in the multi-level structure friends:{
      handler(newValue,oldValue){
        console.log(newValue,oldValue,"aa")
      },
      deep:true, //enable deep monitoring}
  }

insert image description here

Note : There is a problem here. When deep monitoring occurs, the new and old values ​​will be the same?

Official explanation:

When mutating (not replacing) an object or array, the old value will be identical to the new value, since their references point to the same object/array. Vue does not keep a copy of the value before mutation

This causes the pointer to change. In my article "Do You Know How to Deep Copy JS?", I talked about data storage.

Call now

Same as deep configuration location.

immediate is set to true to trigger the current handler callback immediately

  watch:{
    name:{
      handler(newValue,oldValue){ //newValue new value, oldValue value before change console.log(newValue,oldValue)
        this.friends.friend_1='Wang Wu'
      },
      immediate:true
    },
  }

It is executed once when the page is loaded, so the old data is undefined

insert image description here

Summarize

The watch listening property can usually be used for data persistence, event dispatching and synchronous/asynchronous execution, format verification...

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Vue monitoring attribute graphic example
  • Vue2 monitors attribute changes watch example code
  • Detailed explanation of the use of Vue2.0 monitoring attributes and the use of calculated attributes
  • Using Vue.js to monitor attribute changes

<<:  Three ways to achieve background blur in CSS3 (summary)

>>:  Problems and solutions for deploying Nginx+KeepAlived cluster dual-active architecture in VMware

Recommend

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Solution to MySQL unable to read table error (MySQL 1018 error)

1. Error reproduction I can access the MySQL data...

VMware ESXi 5.5 deployment and configuration diagram process

Table of contents 1. Installation requirements 2....

select the best presets to create full compatibility with all browsersselect

We know that the properties of the select tag in e...

Docker FAQ

Docker only maps ports to IPv6 but not to IPv4 St...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

How does MySQL achieve master-slave synchronization?

Master-slave synchronization, also called master-...

Centos6.5 glibc upgrade process introduction

Table of contents Scenario Requirements glibc ver...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...

A universal nginx interface to implement reverse proxy configuration

1. What is a proxy server? Proxy server, when the...

js array fill() filling method

Table of contents 1. fill() syntax 2. Use of fill...

MySQL 5.7.18 release installation guide (including bin file version)

The installation process is basically the same as...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...