1.前言
(a)每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期;
(b)vue一整个的生命周期中会有很多钩子函数提供给我们在vue生命周期不同的时刻进行操作;
(c)钩子函数中最为常用的是创建(created),挂载(mounted)的操作;
2.详解
(a)图示周期
(b)各阶段详解
beforeCreate | 在实例初始化之后,数据观测和事件、生命周期初始化配置之前被调用。 |
created | 实例已经创建完成之后被调用。在这一步,实例已完成以下的配置:数据观测,属性和方法的运算,事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。 应用场景:ajax请求获取数据,初始化数据 |
beforeMount | 在挂载开始之前被调用:相关的 render 函数首次被调用,此时有了虚拟DOM。 |
mounted | el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子,渲染为真实DOM。 应用场景:挂载元素内DOM节点的获取 |
beforeUpdate | 在数据更新之前时调用,发生在虚拟 DOM 重新渲染和打补丁之前。 你可以在这个钩子中进一步地更改状态,这不会触发附加的重渲染过程。 |
updated | 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。 当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态,因为这可能会导致更新无限循环。 应用场景:任何数据的更新,如需统一逻辑处理 |
beforeDestroy | 实例销毁之前调用。此时,实例仍然是可用的。 |
destroyed | vue 实例销毁后调用。调用后,vue 实例指示的所有东西都会解绑,所有的事件监听器会被卸载移除,所有的子实例也会被销毁。 |
(c)代码使用
<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) //undefined
},
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 : " + (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 挂载结束状态,ajax调用走起------');
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: function () {
console.group('beforeUpdate 更新前状态===============》');
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);
},
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>