1.今天看了篇文章写的挺好,结合下面的评论,总结了下。
详解vue生命周期segmentfault.com2.稍微了改了一下下的代码,直接html打开就能看到了。
原先的代码,可能因为console.log,打印的问题会造成一些困惑。
<!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>vue生命周期学习</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
</head>
<body>
<div id="app">
<h1>{{message}}</h1>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Vue的生命周期'
},
beforeCreate: function() {
console.group('------beforeCreate创建前状态------');
console.log("%c%s", "color:red" , "el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //undefined
console.log("%c%s", "color:red","message: " + this.message)
},
created: function() {
console.group('------created创建完毕状态------');
console.log("%c%s", "color:red","el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
console.log("%c%s", "color:red","message: " + this.message); //已被初始化
},
beforeMount: function() {
console.group('------beforeMount挂载前状态------');
console.log("%c%s", "color:red","el : " + (JSON.stringify(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: function() {
console.group('------mounted 挂载结束状态------');
console.log("%c%s", "color:red","el : " + 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); //已被初始化
},
beforeUpdate: function () {
console.group('beforeUpdate 更新前状态===============》');
console.log("%c%s", "color:red","el : " + 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: function () {
console.group('updated 更新完成状态===============》');
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);
},
beforeDestroy: function () {
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: function () {
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)
}
})
</script>
</html>
3. vue生命周期的钩子函数
8个钩子函数
- beforeCreate
- created
- beforeMounted
- mounted
- beforeUpdated
- Updated
- beforeDestory
- destoryed
结合一张图片
4. 总结
- 创建前,this指向创建的实例,此时,data,methods 等都不能访问
- 实例创建完成,此时可以访问,data,methods,等方法,此时只是创建了vue实例,还未进行挂载,也就没有$ref,$el
- 在挂载之前,找到对应的模板,编译成render函数
- 挂载后,也就是把虚拟dom替换成了真实的dom,此时可以访问$fef 和$el
- 数据响应更新前的钩子函数,可以监听到data的变化,但不进行渲染
- 虚拟dom重新渲染数据,更新到view视图
- 实例销毁前,还可以操作实例
- 销毁实例后调用
1.beforeMount时,$el是没有数据的,Mounted后,数据渲染到了视图上。
2.beforeUpdate时,数据更新了但没渲染,只有到了updated时,数据才重新渲染到了view视图。