1. Component initialization
- Merge
options
: merge the constructoroptions
with theVue
constructoroptions
or remove the constructoroptions
For details, see the chapter on parameter merging
- Initialization data: as
props
,data
,methods
etc. - Initialization is complete: call the
created
hook
2. Component mounting
- Definition update function:
updateComponent
method
The updateComponent method is divided into two parts:
- Render part: generate vnode according to the render function, and generate mount vnode or component placeholder for sub-components.
- Update part: Generate dom nodes based on vnode generated by render, and add attribute binding events for dom nodes.
- New rendering
watcher
: the methodwatcher
will be called every time it is triggeredupdateComponent
to recalculate the current componentvnode
and update the page - When updating the page (
update
stage), if it encounters a mountvnode
, itvnode
is a component, the component will be initialized, and the above steps will be repeated
3. Component update (diff)
- The page update of the parent component is triggered first. When the parent component executes the
diff
algorithm, itvnode
will modify theprops
value of the child component when the component is mounted . - Modifying the
props
value triggers the update of the sub-component page. Due towatcher
the asynchronous nature, the update will be performed in the next event loop - Compare whether the root node is equal, create a new one if it is not equal,
dom
and execute thediff
algorithm of the sub-component if it is equal
4. Parameter combination (mixin)
mixin
In the phase, theVue
constructor willmixin
mount the configuration items under its ownoptions
properties.- For the root component, merge the configuration items of the
Vue
constructoroptions
with its own configuration items in the initialization phase , and each attribute has a different merging strategy. - For sub-components, the
vnode
constructor of the current component will be built when the mount is created . After the constructor is built, the merge process will be executed to merge its configuration withVue
theoptions
properties of the constructor .
5. Asynchronous Components
webpack
Syntax for the loading of asynchronous components- In the
vnode
process of building and mounting the subcomponent , thecreateComponent
method is called createComponent
The method will render the content in the constructor according to the returned constructor- Advanced asynchronous components use this method to return a different constructor to achieve
loading
,error
rendering the component itself
6. The life cycle sequence of parent and child components
Mounting process
Parent component beforeCreate
Parent component create
Parent componentbeforeMount
Sub-component beforeCreate
sub-component create
sub-component beforeMount
sub-componentmounted
The mounted
update process of the
parent component The parent component The beforeUpdate
child component The beforeUpdate
child updated
component The updated
destruction process of the
parent component The parent component The beforeDestroy
child component The beforeDestroy
child component The destroyed
parent componentdestroyed
For mounted
, updated
, destroyed
process, are all parent component first call before
after the hook, and then passed to the sub-components, sub-assemblies to call a complete hook, then called by the parent component ed
hook