A Vue
instance needs to go through a series of processes such as creation, data initialization, template compilation, DOM
mounting, rendering, updating, rendering, and unmounting. This process is the lifecycle of Vue
, and it provides hook functions such as beforeCreate
, created
, beforeMount
, mounted
, beforeUpdate
, updated
, beforeDestroy
, and destroyed
. When parent and child components are nested, each has its own independent hook functions.
The creation process mainly involves four hook functions: beforeCreate
, created
, beforeMount
, and mounted
.
The update process mainly involves two hook functions: beforeUpdate
and updated
, and lifecycle comparison only occurs when there is data transmission between parent and child components.
The destruction process mainly involves two hook functions: beforeDestroy
and destroyed
. In this example, calling vm.$destroy()
directly destroys the entire instance to achieve the purpose of destroying parent and child components.
Vue
lifecycle hook function example, where this.msg
is initially assigned Vue Lifecycle
, and is assigned Vue Update
during the update process.
The process from the creation of the Vue
instance to the execution of the beforeCreate
hook mainly involves some initialization operations, such as the initialization of component events and lifecycle hooks. When this lifecycle hook is executed, the component is not yet mounted, and data
, methods
, etc. are not yet bound. This phase can mainly be used to perform operations unrelated to Vue
data, such as displaying a loading
indicator.
The process from beforeCreate
to created
mainly involves configuring data binding, mounting computed properties and methods, and defining watch/event
callbacks. When this lifecycle hook is executed, the component is not yet mounted to the DOM
, and the property $el
is still undefined
, but it is already possible to start accessing data
and methods
, although the page has not been rendered yet. In this stage, it is usually used to initiate an XHR
request.
The process from created
to beforeMount
mainly involves parsing the page template, resolving the page's data and directives in memory. When the page is fully parsed, the page template exists in memory. When this lifecycle hook is executed, $el
is created, but the page exists only in memory and has not yet been rendered to the DOM
.
The process from beforeMount
to mounted
involves rendering the page from memory to the DOM
. When this lifecycle hook is executed, the page has already been rendered, and the component has completed the last hook of the creation stage and is about to enter the running stage. Additionally, regarding the priority of the rendered page template, it is render
function >
template
property >
external HTML
.
When data is updated, the beforeUpdate
hook is called. At this point, the data in the Vue
instance is already up to date, but the data in the page is still old. At this time, further state changes can be made without triggering an additional re-rendering process. In the example above, adding a debugger
breakpoint allows you to observe that the data in the Vue
instance is already up to date, but the data on the page is still old.
When data is updated and the DOM
is rendered, the updated
hook is called. At this point, the component's DOM
has been updated, and operations dependent on the DOM
can be performed.
The beforeDestroy
hook is called before the Vue
instance is destroyed, and at this time the instance is still fully available.
The destroyed
hook is called after the Vue
instance is destroyed. At this time, all things bound to the Vue
instance will be unbound, all event listeners will be removed, all child instances will be destroyed, and the component will be unusable. Data
and methods
will also be unusable. Even if the instance's properties are changed, the page's DOM
will not be re-rendered.