How does Vue track data changes?

How does Vue track data changes?

background

This situation occurs from time to time at work: the data displayed on the page is incorrect and needs to be located by front-end colleagues.

In a SPA application built with Vue, the data finally displayed on the page may be simple or complex from initialization to final display. When you encounter complex data flows, it will be a headache to troubleshoot without a suitable method.

If you can see the call stack when the data changes, you can know what happened before the wrong data was generated and which step of the error led to the final error. By following the clues given by the call stack, you can quickly locate the problem.

example

<template>
  <div>
    <!-- Expected output: hello,world -->
    <!-- Actual output: hello,woold -->
    {{ msg }}
    <button @click="f1">change msg</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      msg: 'hello,',
    }
  },
  methods: {
    f1() {
      this.msg += 'w'
      this.f2();
    },
    f2() {
      this.msg += 'oo';
      this.f3();
    },
    f3() {
      this.msg += 'ld'
    }
  }
};
</script>

Misconception - Setting breakpoints in Watch to view the call stack

We can see the callbacks of f1 and msg in the Call Stack on the right side of the page, but we cannot see f2 and f3. That is to say, f2 and f3 are lost, but in fact it is f2 that causes the data error.

Why is the calling information of f2 and f3 lost?

Because f1, f2, and f3 all modified msg, the Watcher of msg was triggered in the same microtask. Since f1 was triggered first, the triggering of f2 and f3 was invalid. When the Watcher callback finally runs, it only remembers that f1 triggered it, so the Call Stack at this time can only see the information of f1.

The right approach

Go to node_modules\vue\dist\vue.runtime.esm.js and add a breakpoint in the set method of defineReactive function. The key here is the name of the variable to be monitored.

Here, you can see the complete process of msg changing and quickly locate the problem caused by f2.

Summarize

By viewing the call stack, you can quickly locate where the data error occurs without being familiar with the project. Compared to using console.log or spending a lot of time to sort out the code logic, the data flow method can reduce a lot of workload.

The above is the details of how Vue tracks data changes. For more information about Vue tracking data changes, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the implementation principle of Vue2.0/3.0 two-way data binding
  • Analysis on the problem of data loss caused by forced refresh of vuex
  • Vue+canvas realizes the effect of refreshing waterfall chart from top to bottom in real time (similar to QT)
  • Solution to Vue data assignment problem
  • Vue resets data to its initial state
  • Analysis and solution of data loss during Vue component value transfer
  • SpringBoot+Vue realizes data adding function
  • Handwritten Vue2.0 data hijacking example
  • Vue data two-way binding implementation method
  • Avoid abusing this to read data in data in Vue
  • Design a data collector with vue

<<:  Solution to the error when installing Docker on CentOS version

>>:  Docker creates MySQL explanation

Recommend

MySQL Best Practices: Basic Types of Partition Tables

Overview of MySQL Partitioned Tables As MySQL bec...

Detailed explanation of js closure and garbage collection mechanism examples

Table of contents Preface text 1. Closure 1.1 Wha...

Detailed explanation of fetch network request encapsulation example

export default ({ url, method = 'GET', da...

jQuery to achieve the barrage effect case

This article shares the specific code of jQuery t...

Steps to install MySQL 8.0.23 under Centos7 (beginner level)

First, let me briefly introduce what MySQL is; In...

Detailed explanation of how to customize the style of CSS scroll bars

This article introduces the CSS scrollbar selecto...

MySQL Server 8.0.3 Installation and Configuration Methods Graphic Tutorial

This document records the installation and config...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...

A brief talk about Rx responsive programming

Table of contents 1. Observable 2. Higher-order f...

JavaScript to implement a simple clock

This article example shares the specific code for...

How to set a fixed IP address in CentOS7 virtual machine

Since my development environment is to install Ce...

Vue implements simple production of counter

This article example shares the simple implementa...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

Detailed explanation of Vue3's sandbox mechanism

Table of contents Preface Browser compiled versio...