backgroundThis 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 stackWe 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 approachGo 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. SummarizeBy 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:
|
<<: Solution to the error when installing Docker on CentOS version
>>: Docker creates MySQL explanation
1. Download the ubuntu16.04 image and the corresp...
Table of contents redo log Why do we need to upda...
1》Be good at web design 2》Know how to design web p...
Code first, then text Copy code The code is as fol...
Table of contents 1. Array deduplication 2. Dedup...
This article shares with you how to use canvas an...
Copy code The code is as follows: <thead> &...
Switching files is a common operation in Linux. W...
Table of contents Undo Log Undo Log Generation an...
1. flex-direction: (direction of element arrangem...
Recently, I plan to deploy a cloud disk on my hom...
There are three ways to start a springboot projec...
This article shares the installation steps of MyS...
Most of the time, plug-ins are used to upload fil...
HTML5 is the next version of the HTML standard. M...