Analysis of the source code of vue data control view
Jun 29, 2018 pm 05:42 PMThis article mainly introduces the analysis of the vue data control view source code. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
Analysis of how vue implements data changes and updates view.
Preface
Three months ago I read the vue source code to analyze how to achieve responsive data. The article is called vue source code for responsive data. , and finally analyzed that the update() method of Watcher will be called after the data changes. So let’s continue to see what update() does after three months. (In the past three months, I have used react-native to build a project, but I have no intention of doing so. Summarized, because it seems too simple).
The narrative method of this article is to follow the logic of looking at the source code. The version of vue I checked is 2.5.2. I forked a copy of the source code. Used to record comments.
Purpose
Only by clarifying the direction of investigation can we reach the goal. Let’s talk about the target behavior first: what method is executed to update the view after the data changes. Then prepare to start looking for answers in this direction and start from the entrance of vue source code.
Start from the previous conclusion
Let’s review the previous conclusion first:
When Vue is constructed, an Observer object will be created on the data (and some other fields). The getter and setter are intercepted. The getter triggers dependency collection and the setter triggers notify.
Another The object is Watcher. When registering a watch, the watch object will be called once, which triggers the getter of the watch object and collects the dependencies into the deps of the current Watcher. When any setter of the dep is triggered, the current Watcher will be notified to call the update of the Watcher. () method.
Then here we start by registering the rendering-related Watcher.
The file is found in src/core/instance/lifecycle.js.
1 |
|
mountComponent
The rendering-related Watcher is called in the mountComponent() method, so let’s search where this method is called. There are only 2 places, respectively. They are src/platforms/web/runtime/index.js and src/platforms/weex/runtime/index.js. Take web as an example:
1 2 3 4 5 6 7 |
|
It turns out that it is the $mount() method that calls mountComponent( ), (or specifying the el field during vue construction will automatically call the $mount() method), because the rendering objects of web and weex (what is weex? I have introduced it in other articles before) are different, so when publishing, it should be After introducing different files, different dists cannot be sent (this problem is left to study the entire process of vue later).
The following is the mountComponent method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
This code actually only does 3 things:
Call the beforeMount hook
Create Watcher
Call the mounted hook
(Hahaha) So actually the core is to create a Watcher.
Look at the parameters of the Watcher: vm is this, updateComponent is a function, noop is empty, null is empty , true means RenderWatcher.
Looked at isRenderWatcher in Watcher:
1 2 3 |
|
Yes, I just made a copy to judge something when the watcher patches for the first time (from the comments I read it in, I still don’t know what it is for).
The only problem that has not been solved is what updateComponent is.
updateComponent
If the function is passed as the second parameter of the Watcher constructor, then this function becomes the getter of the watcher. If you are smart, you should have guessed that all the data in the view must be called in this updateComponent. Getter can establish dependencies in the watcher so that the view can respond to data changes.
1 2 3 |
|
Then go to vm._update() and vm._render().
In src/core/ instance/render.js found the ._render() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
This method does:
Generate VNode based on the render method of the current vm. (render method It may be compiled based on the template or vue file, so it is inferred that writing the render method directly is the most efficient)
If there is a problem with the render method, then call the renderError method first, and if it fails, read it. The vnode of this time is either null.
If there is a parent node, put it in its own .parent attribute.
Finally return VNode
So the core is this sentence:
1 |
|
I don’t know what render(), vm._renderProxy, vm.$createElement are.
First Look at vm._renderProxy: it is set during initMixin(). It returns vm in the production environment and returns the proxy in the development environment. Then we think it is a vm that can be debugged (that is, vm). We will look at the details later.
## The code of #vm.$createElement is in the vdom folder. After looking at it, it is a method that returns a VNode. render is a bit complicated. Is it possible to study it later? In short, it is to combine the template or vue single file with mount The target parse becomes the render function.Small summary: The return value of vm._render() is VNode, based on the render function of the current vmLet’s look at vm._update()1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Conclusion
Vue's view rendering is a special kind of Watcher. The content of the watch is a function. The render function is called during the running process of the function. The render is compiled by the template or el's dom (the template contains some observed objects). data). Therefore, changes in the observed data in the template trigger the Watcher's update() method to re-render the view.
Legacy
Where is the render function compiled?
What is the process of introducing different platforms when vue source code is released and finally typing it into dist
Analysis of __patch__ and VNode
The above is the entire content of this article. I hope it will be useful to you. Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About Vue’s ideas for solving cross-domain routing conflicts
About Vue’s dynamic setting of routing parameters Introduction
#About vue virtual dom patch source code analysis
The above is the detailed content of Analysis of the source code of vue data control view. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The difference between event and $event in vue

The difference between export and export default in vue

What scenarios can event modifiers in vue be used for?
