先放一张官方文档图:
钩子函数
从图中可以看到Vue生命周期中的几个钩子函数
Vue2.0生命周期钩子函数 | 描述 |
beforeCreate | 组件实例刚被创建,$data、$el等都为undefined |
created | 组件实例创建完成,$data数据已经绑定,但$el尚未声明,为undefined |
beforeMount | 组件挂载之前,$el属性已经生成,其中DOM使用的data变量数据尚未被替换 |
mounted | 组件挂载之后,$el属性已经生成,文档DOM中el元素被新创建的 $el 替换,并挂载到实例上去之后调用该钩子 |
beforeUpdate | 组件更新之前,数据更新时调用,发生在虚拟 DOM 重新渲染和打补丁之前。你可以在这个钩子中进一步地更改状态,这不会触发附加的重渲染过程。 |
updated | 组件更新之后,组件 DOM 已经更新,此时可以操作更新后的DOM |
activated | keep-alive 组件被激活时使用,会从缓存中读取组件状态 |
deactivated | keep-alive 组件停用时调用。 |
beforeDestroy | 实例销毁之前 |
destroyed | Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。 |
实例
以下实例来自详解vue生命周期
<!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>
<div id="test">{{test}}</div>
</body>
<script>
let test = new Vue({
el: '#test',
data: {
test: 'test'
}
})
let app = 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(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 挂载结束状态------');
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);
console.log('beforeUpdate == ' + document.getElementsByTagName('h1')[0].innerHTML);
},
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);
console.log('updated == ' + document.getElementsByTagName('h1')[0].innerHTML);
},
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>
beforeCreate与created:
可以看到在beforeCreate函数时,el与data属性都未创建定义,在created函数中,data属性数据已经绑定,el属性尚未被创建,此时可以在控制台通过app.$data获取到message变量
beforeMount与mounted:
在beforeMount钩子函数中,data、el都已经被定创建定义,el中使用data数据尚未被替换,在mounted中所有的组件数据都已经替换更新,此时DOM中的message已经变成 "Vue的生命周期",这里就是应用Virtual DOM
(虚拟Dom)技术。
beforeUpdate与updated:
此时在控制台执行
app.message = "vue test"
当data属性数据更改,则触发update系列钩子函数,可以看到控制台打印的两次el元素都一样,是因为此时打印的是虚拟DOM
通过 beforeUpdate == Vue的生命周期 与 updated == vue test 看出在beforeUpdate时,el与data都已经更新,但是组件尚未被挂载都页面DOM中,在updated中,组件实例已经挂载。
beforeDestroy与destroyed:
在控制台执行下面代码,来销毁组件
app.$destroy();
在beforeDestroy函数时,还可以获取页面组件DOM、数据等
destroyed钩子函数在Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。即Vue组件不在响应
我们在控制台再次修改message,但是不在触发vue组件钩子函数,页面也不在更新
以上是我对Vue生命周期的一下理解,欢迎指正~
每天记录一点点,笨鸟先飞
参考文章: