Vue生命周期

引言

在Vue实例的创建、运行、销毁期间,总是伴随着各种各样的事件,这些事件统称为”生命周期”

代码示例

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<div id="app" @click="change">
<p>{{ message }}</p>
</div>
</template>

<script>
export default {
name: "App",
data() {
return {
message: "hello world!",
};
},
beforeCreate() {
console.group("beforeCreate 创建前状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
created() {
console.group("created 创建完毕状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
beforeMount() {
console.group("beforemount 挂载前状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
mounted() {
console.group("mounted挂载结束状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
beforeUpdate() {
console.group("beforeUpdate更新前状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log(this.$el.innerHTML);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
updated() {
console.group("Updated更新完成状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log(this.$el.innerHTML);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
beforeDestroy() {
console.group("beforeDestroy销毁前状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
destroyed() {
console.group("destroyed销毁完成状态");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
methods: {
change() {
this.message = "Hello vue!";
console.group("点击事件执行的方法");
console.log("%c%s", "color:red", "el :" + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red", "data :" + this.$data);
console.log("%c%s", "color:red", "message:" + this.message);
},
},
};
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

beforeCreate()

在实例初始化后,数据观测(data observer)和event/watcher事件配置前被调用

这个时候this还不能使用,data中的数据、methods中的方法、以及watcher中的事件都不能获得,值为undefined

beforeCreated

created()

在实例已经创建完成后被调用。在这一步,实例已经完成:数据观测(data observer)、属性和方法的运算及watch/event事件回调

created

beforeMount()

在挂载开始前被调用,相关的render函数首次被调用

beforemount

mounted()

挂载完毕,这时DOM节点被渲染到文档内,DOM操作在此时能正常进行

mounted

beforeUpdate()

在数据更新时被调用,发生在虚拟DOM打补丁前,适合在更新前访问现有的DOM

beforeupdate

updated()

由于数据的更改导致虚拟DOM重新渲染,在这以后会调用该钩子函数

在大多数情况下,应该避免在此期间改变状态。如果要改变相应状态,最好使用计算属性或watcher取而代之

updated不会承诺所有子组件都一起被重绘

如果希望等到所有视图都重绘完毕,可以使用$nextTick

beforeUpdate()和updated()钩子函数中的$el一样,因为beforeUpdate应该指向虚拟DOM,所以$el相同,而DOM中的真正内容是不一样的

updated

beforeDestroy()

在实例销毁前被调用,这一步,实例仍然完全可用

destroyed()

在Vue实例销毁后调用。调用后,Vue实例指向的所有部分都会被解绑定、所有的事件监听器会被移除、所有的子实例也会被销毁

总结

img


Vue生命周期
https://blog-theta-ten.vercel.app/2021/08/26/Vue生命周期/
作者
Chen
发布于
2021年8月26日
许可协议