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

Understand all aspects of HTTP Headers with pictures and text

What are HTTP Headers HTTP is an abbreviation of ...

Implementation of dynamic rem for mobile layout

Dynamic rem 1. First, let’s introduce the current...

Steps to build the vite+vue3+element-plus project

Use vite to build a vue3 project You can quickly ...

Markup Language - Phrase Elements

Click here to return to the 123WORDPRESS.COM HTML ...

How to install and modify the initial password of mysql5.7.18

For Centos installation of MySQL, please refer to...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

How to install mysql5.7 in windows

First download the compressed version of mysql, t...

CSS horizontal progress bar and vertical progress bar implementation code

Sometimes it’s nice to see some nice scroll bar e...

Summary of MySQL character sets

Table of contents Character Set Comparison Rules ...

MySQL Optimization Summary - Total Number of Query Entries

1. COUNT(*) and COUNT(COL) COUNT(*) usually perfo...

Three ways to configure Nginx virtual hosts (based on domain names)

Nginx supports three ways to configure virtual ho...

Detailed steps to download Tomcat and put it on Linux

If you have just come into contact with Linux, th...

How to simulate enumeration with JS

Preface In current JavaScript, there is no concep...

HTML design pattern daily study notes

HTML Design Pattern Study Notes This week I mainl...