由于官方版本生命周期图示有点儿大,拉过来会不清晰,图示直接点击链接吧:生命周期图示
下面我们将结合代码去看看钩子函数的执行:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1 id="h1">{{message}}</h1>
<input type="button" @click="change" value="更新数据" />
<input type="button" @click="destroy" value="销毁" />
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Welcome Vue',
},
methods: {
show() {
console.log('执行了show方法')
},
change() {
this.message = '我被改变了';
},
destroy() {
vm.$destroy();
}
},
beforeCreate() {
//在此生命周期执行时,data和methods中的数据都还未初始化
console.group('beforeCreate 创建前状态===============》');
console.log("%c%s", "color:red", "el : " + this.$el); //undefined
console.log("%c%s", "color:red", "message: " + this.message);//undefined
this.show();//this.show is not a function
},
created: function () {
//如果想调用data和methods中的数据,至少要在此生命周期执行
console.group('created 创建完毕状态===============》');
console.log("%c%s", "color:red", "el : " + this.$el); //undefined
console.log("%c%s", "color:green", "message: " + this.message); //Welcome Vue => 已被初始化
this.show();//执行了
},
beforeMount: function () {
//此时页面中的元素还没真正替换过来
console.group('beforeMount 挂载前状态===============》');
console.log("%c%s", "color:green", "el : " + (this.$el)); //已被初始化
console.log(this.$el); // 当前挂在的元素
console.log("%c%s", "color:red", document.getElementById('h1').innerText); //{{message}}
},
mounted: function () {
console.group('mounted 挂载结束状态===============》');
console.log("%c%s", "color:green", document.getElementById('h1').innerText); //Welcome Vue
},
beforeUpdate: function () {
//此阶段页面中显示的是旧数据,data中的是最新数据,页面尚未和最新数据同步
console.group('beforeUpdate 更新前状态===============》'); //这里指的是页面渲染新数据之前
console.log("%c%s", "color:green", "界面上元素内容: " + document.getElementById('h1').innerText); //Welcome Vue
console.log("%c%s", "color:green", "message: " + this.message);//我被改变了
},
updated: function () {
//此时页面和data中的数据保持一致,都是最新的
console.group('updated 更新完成状态===============》');
console.log("%c%s", "color:green", "界面上元素内容: " + document.getElementById('h1').innerText); //我被改变了
console.log("%c%s", "color:green", "message: " + this.message);//我被改变了
},
beforeDestroy: function () {
//此时data和methods中的数据都处于可用状态
console.group('beforeDestroy 销毁前状态===============》');
},
destroyed: function () {
console.group('destroyed 销毁完成状态===============》');
}
})
</script>
</body>
</html>
1、初始化方法
一开始没做任何操作时,页面渲染出来的结果如下图所示:
总结:
beforeCreate :el 和 data 并未初始化,methods 中的方法不可用。
created :完成了 data 数据的初始化,methods 中的方法也可用,el没有初始化。
beforeMount :完成了 el 和 data 初始化,页面上的元素内容还是“{{message}}”。
mounted :完成挂载。
2、数据更新方法
当点击页面上的“更新数据”时,控制台又打印如下内容:
总结:
beforeUpdate :此阶段页面中显示的是旧数据,data中的是最新数据,页面尚未和最新数据同步。
updated :此时页面和data中的数据保持一致,都是最新的数据。
3、销毁方法
当点击页面上的“销毁”时,控制台又打印如下内容:
总结:
beforeDestroy :此时data和methods中的数据都处于可用状态。
destroyed:此时页面销毁完成。
启蒙链接:https://blog.youkuaiyun.com/qq_15766181/article/details/73549933