学习了Vue2.0,但对Vue2.0生命周期一直不是很理解,今天来梳理一下。
首先上图:
可以看到分为:
beforeCreate
在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用。
也就是说现在还不能使用data
created
实例创建完成,属性也已经绑定,但是dom还未生成
实例已经创建完成之后被调用。在这一步,实例已完成以下的配置:数据观测(data observer),属性和方法的运算, watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。
beforeMount
模板编译完成
在挂载开始之前被调用:相关的 render 函数首次被调用。
mounted
模板编译完成,挂载完毕
el 被新创建的 vm.
el替换,并挂载到实例上去之后调用该钩子。如果root实例挂载了一个文档内元素,当mounted被调用时vm.
el 也在文档内。
beforeUpdate
数据更新时调用,发生在虚拟 DOM 重新渲染和打补丁之前。你可以在这个钩子中进一步地更改状态,这不会触发附加的重渲染过程。
updated
由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。
当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态,因为这可能会导致更新无限循环。
beforeDestroyed
实例销毁之前调用。在这一步,实例仍然完全可用。
destroyed
Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。
actived
keep-alive 组件激活时调用。
deactivated
keep-alive 组件停用时调用。
以上就是各个钩子函数的调用时间
下面使用代码来验证一下
var app = new Vue({
el:"#app",
data:{
message:'vue生命周期探究'
},
beforeCreate:function(){
console.log('beforeCreate:===================================');
console.log('this.$el:'+this.$el);//undefined
console.log('this.data:'+this.$data);//undefined
console.log('this.message'+this.message);//undefined
},
created:function(){
console.log('created:===================================');
console.log('this.$el:'+this.$el);//undefined
console.log('this.data:'+this.$data);//[object Object]
console.log('this.message'+this.message);//vue生命周期探究
},
beforeMount:function(){
console.log('beforeMount:===================================');
console.log('this.$el:'+this.$el);//[object HTMLDivElement]
console.log('this.data:'+this.$data);//[object Object]
console.log('this.message'+this.message);//vue生命周期探究
},
mounted:function(){
console.log('mounted:===================================');
console.log('this.$el:'+this.$el);//[object HTMLDivElement]
console.log('this.data:'+this.$data);//[object Object]
console.log('this.message'+this.message);//vue生命周期探究
},
beforeUpdate:function(){
console.log('beforeUpdate:===================================');
console.log('this.$el:'+this.$el);
console.log('this.data:'+this.$data);
console.log('this.message'+this.message);
},
updated:function(){
console.log('updated:===================================');
console.log('this.$el:'+this.$el);
console.log('this.data:'+this.$data);
console.log('this.message'+this.message);
},
beforeDestroy:function(){
console.log('beforeDestroy:===================================');
console.log('this.$el:'+this.$el);
console.log('this.data:'+this.$data);
console.log('this.message'+this.message);
},
Destroyed:function(){
console.log('Destroyed:===================================');
console.log('this.$el:'+this.$el);
console.log('this.data:'+this.$data);
console.log('this.message'+this.message);
}
});
create和mount
beforecreated:el 和 data 并未初始化
created:完成了 data 数据的初始化,el没有
beforeMount:完成了 el 和 data 初始化
mounted :完成挂载
打开浏览器看运行结果:
update 相关
接着在控制台输入:
app.message='修改';
就能看到data里的值被修改后,将会触发update的操作。
destroy相关
接着在控制台输入:
app.$destroy();
我们在console里执行下命令对 vue实例进行销毁。销毁完成后,我们再重新改变message的值,vue不再对此动作进行响应了。但是原先生成的dom元素还存在,可以这么理解,执行了destroy操作,后续就不再受vue控制了。