Interview question:
new Vue
What happened after that? What happened after the data changed?
-
The process of creating a Vue instance and creating a component are basically the same.
-
First do some initialization operations, mainly setting some private properties to the instance.
-
Run life cycle hook function
beforeCreate
-
Enter the injection process: process attributes, computed, methods, data, provide, inject, and finally use the proxy mode to mount them into the instance
-
Run life cycle hook function
created
-
Generate
render
function: If there is a configuration, use the configured one directlyrender
. If not, use the runtime compiler to compile the template intorender
-
Run life cycle hook function
beforeMount
-
Create one
Watcher
, pass in a functionupdateComponent
, the function will run , and then pass therender
result into the function for execution.vnode
_update
During
render
the execution of the function, all dependencies will be collected, andupdateComponent
the function will be re-run when the dependencies change in the future.During the execution
_update
of the function,patch
the function is triggered. Since there is currently no old tree, the elm attribute, that is, the real dom, is directly generated for each ordinary node of the current virtual dom tree.If you encounter a vnode that creates a component, you will enter the component instantiation process, which is basically the same as the process of creating a vue instance. Finally, the created component instance will be mounted in the
componentInstance
properties of the vnode for reuse. -
Run life cycle hook function
mounted
-
-
Re-render?
-
After the data changes, everything that depends on the data
Watcher
will be re-run. OnlyupdateComponent
the functions corresponding to the function are considered here.Watcher
-
Watcher
It will be run by the schedulernextTick
, that is, in a micro queue. This is to avoid multiple dependent data being executed multiple times after being changed at the same time. -
Run life cycle hook function
beforeUpdate
-
updateComponent
Function re-executionDuring the execution
render
of the function, the previous dependencies will be removed and all dependencies will be re-collected.updateComponent
The function will be re-run when the dependencies change in the future.During
_update
the execution of the function,patch
the function is triggered.Compare the old and new trees.
Comparison of ordinary
html
nodes will cause real nodes to be created, deleted, moved, and updated.Comparison of component nodes will cause components to be created, deleted, moved, and updated
When a new component needs to be created, enter the instantiation process
When an old component needs to be deleted, the old component's
$destroy
method will be called to delete the component. This method will first trigger the life cycle hook functionbeforeDestroy
, then recursively call the child component's$destroy
method, and then trigger the life cycle hook function.destroyed
When the component properties are updated,
updateComponent
the function equivalent to the component is retriggered and executed, entering the re-rendering process, the same as this section. -
Run life cycle hook function
updated
-