Life cycle process
$mount() method : The role is the
el
same as the property, which can render the vue instance into the view container
template attribute : If this attribute is set, it will
template
be rendered as the content of the page when rendering on the page in the future
1. Create a vue instance
2. Initialize event & life cycle
- When creating a vue instance, this object is actually empty, and only at this point does it slowly start to execute some content
3. Initialize the data and method in the instance
- Before this step, data and method actually have no content
4. Determine and generate virtual DOM
- Determine whether there is an el attribute: (el: whether the specified rendering container)
- if it exists:
- Continue down execution
- If it does not exist:
- When the
$mount
method is called , continue to execute downward
- When the
- if it exists:
- Determine whether there are
template
attributes in the instance- if it exists:
- Generate a virtual DOM from the corresponding content in the template attribute
- If it does not exist:
- The
<div id="app"></div>
contents generate a virtual DOM
- The
- if it exists:
5. Render the virtual DOM onto the page
6. The page will enter the mounted state and wait for the data in data to change
- If the data in data changes:
- Update the virtual DOM, re-render it to the page, and enter the mounted state again
7. Destroy the vue instance
- When
$destroy方法
called, it will be destroyed - When switching from one component to another. The previous instance of the vue component will also be destroyed
important point:
el
: Whether to specify the rendered containerel
It$mount
is the same as in fact, it is used to specify the rendering container of the pagetemplate
:- If vue instance does not exist, directly to
<div id=“app”></div>
the rendered content to the page - If the vue instance exists, the content in the template attribute will be directly rendered on the page
- If vue instance does not exist, directly to
虚拟DOM
:- It is equivalent to rendering the content to the page once
打草稿
(there are still instructions)
- It is equivalent to rendering the content to the page once
Life cycle hook function
Execute when initializing data&method
- Before: beforeCreate
- Unable to access
data
&methods
content
- Unable to access
- After: created
- Have access to all
data
andmethod
content - Used for :
- Initialize certain attribute values
- Have access to all
Execute when the virtual DOM is rendered on the page
- Before: beforeMount
- After
虚拟DOM
rendering to the page之前
execution, getDOM结构
and指令操作符
- After
- After: mounted
- In
虚拟DOM渲染
the之后
execution of the page ,指令操作符
all here are replaced with real data (real dom) - Used for:
- After the initialization page is completed, perform some required operations on
html
thedom
nodes.
- After the initialization page is completed, perform some required operations on
- In
Execute when the data before the page is modified
- Before: beforeUpdate
- The data in data has changed, it is executed on
数据渲染
the page之前
(data has changed, dom has not been rendered)
- The data in data has changed, it is executed on
- After: updated
- The data in data has been changed, it is
之后
executed when the data is re-rendered to the page (the data has been changed, and the dom has been re-rendered) - The data displayed on the page and the data in data have been kept in sync, and they are both up to date
- The data in data has been changed, it is
Execute when the vue instance is destroyed
- 之前:beforeDestroy
Vue实例
From entering the operational phase to the phase of the destruction, this time alldata
andmethods
, ,指令
......过滤器
are in a usable state. Hasn't really been destroyed
- After: destroyed
- After the hook is called, all instructions corresponding to the Vue instance are unbound, all event listeners are removed, and all sub-instances are also destroyed.
- At this time, all data and methods, instructions, filters...are in an unavailable state . The component has been destroyed.
- Used for :
- Clear some timers
supplement
- Which hooks will be triggered the first page load?
beforeCreate
,created
,beforeMount
,mounted
<keep-alive>
Lifecycle hookactivated
function: ,deactivated
Is included in the <keep-alive>
hook assembly created, it will be more than two life cycle: activated and deactivated
-
activated: when the component is active call, use
<keep-alive>
will be called when the component is first rendered when, after each<keep-alive>
is called when activated. -
deactivated: called when the component is deactivated.
-
Note : Only the components are
<keep-alive>
parcel, both life-cycle will be called, if used as a normal component, is not being called, and after version 2.1.0, using exclude exclude after, even if wrapped in this The two hooks will still not be called! In addition, this hook will not be called when rendering on the server -
errorCaptured
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
msg: {
{
msg }}
</div>
<input type="text" v-model="msg">
<button @click="msg='修改后的值'">点我修改 msg</button><br />
<button @click="fn">点我销毁</button>
</div>
</body>
<script src="./vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data() {
return {
msg: '修改前的值'
}
},
methods: {
fn() {
this.$destroy()
}
},
beforeCreate() {
console.log('-----------beforeCreate----------')
console.log(this.msg) // undefined
},
created() {
console.log('-----------created----------')
console.log(this.msg) // 修改前的值
},
beforeMount() {
console.log('-----------beforeMount----------')
console.log(this.$el)
},
mounted() {
console.log('-----------mounted----------')
console.log(this.$el)
},
beforeUpdate() {
console.log('-----------beforeUpdate----------')
console.log(this.msg)
},
updated() {
console.log('-----------updated----------')
console.log(this.msg)
},
beforeDestroy() {
console.log('-----------beforeDestroy----------')
},
destroyed() {
console.log('-----------destroyed----------')
}
})
</script>
</html>