vue2.0生命周期(二)

本文深入探讨Vue.js的生命周期,从实例化到销毁的各个阶段,包括beforeCreate、created、beforeMount、mounted等钩子函数的作用及执行时机,并通过示例代码进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Vue对象实例化

1
2
3
4
5
6
7
8
9
10
11
12
JS代码:

new Vue({
/* DOM挂载点 */
el: '#app',
/* 使用哪个模板 */
template: '<div>{{fruit}}</div>',
/* 给模板传递的数据 */
data: {
fruit: 'apple'
}
})

其中Vue函数称为构造函数,使用new就可以实例化出来一个实例化对象。

二、Vue的生命周期

刚刚接触Vue.js, 之前使用React.js知道需要搞清楚它的生命周期及其每个钩子函数的含义。

Vue生命周期.png

钩子函数Description
beforeCreate组件实例刚被创建,组件属性计算之前,如data属性等
created组件实例创建完成,属性已绑定,但DOM还未生成,$el属性还不存在
beforeMount模板编译/挂载之前
mounted模板编译/挂载之后
beforeUpdate组件更新之前
updated组件更新之后
activatedfor keep-alive, 组件被激活时调用
deactivatedfor keep-alive, 组件被移除时调用
beforeDestory组件销毁前调用

2.1生命周期的探究

对于执行顺序和什么时间执行,看上面的图会比较没有太多的耐心,因此先看一下钩子函数的执行然后再回头看图会更容易理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
var app1 = new Vue({
el: '#app',
router,
data: {
message: 'kuaixiang'
},
beforeCreate: function () {
console.group('beforeCreate 创建之前:')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
},
created: function () {
console.group('created 创建完毕:')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
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('%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('%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('%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('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
},
beforeDestory: function () {
console.group('beforeDestory 销毁之前:')
console.log('%c%s', 'color:red', 'el : ' + 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('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
},
template: '<App/>',
components: { App }
})

console.log(app1)

这段代码验证了再Vue.js中的生命周期的每个阶段,需要再结合图来看每个钩子函数。

参考文献

https://segmentfault.com/a/1190000008010666

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值