Lifecycle
# Instance Lifecycle Hooks
Instance Lifecycle Hooks API (opens new window)
Simply put, lifecycle hook functions are functions that a Vue instance automatically executes at certain points in time.
<div id="app">{{msg}}</div>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: 'Vue Lifecycle'
},
beforeCreate: function() {
console.group('------beforeCreate state------');
console.log("el : " + this.$el); //undefined
console.log("data : " + this.$data); //undefined
console.log("msg: " + this.msg) //undefined
},
created: function() {
console.group('------created state------');
console.log("el : " + this.$el); //undefined
console.log("data : " + this.$data); //initialized
console.log("msg: " + this.msg); //initialized
},
beforeMount: function() {
console.group('------beforeMount state------');
console.log(this.$el);// <div id="app">{{msg}}</div> before mount state
},
mounted: function() {
console.group('------mounted state------');
console.log(this.$el);// <div id="app">Vue Lifecycle</div> msg content is mounted and rendered to the page
},
// Before data is modified
beforeUpdate: function () {
console.group('beforeUpdate state===============>');
console.log("el : " + this.$el);
console.log(this.$el);
console.log("data : " + this.$data);
console.log("msg: " + this.msg);
},
// After beforeUpdate is triggered, the virtual DOM re-renders and applies updates
// After data is modified
updated: function () {
console.group('updated state===============>');
console.log("el : " + this.$el);
console.log(this.$el);
console.log("data : " + this.$data);
console.log("msg: " + this.msg);
},
// Before calling vm.$destroy()
beforeDestroy: function () {
console.group('beforeDestroy state===============>');
console.log("el : " + this.$el);
console.log(this.$el);
console.log("data : " + this.$data);
console.log("msg: " + this.msg);
},
// After calling vm.$destroy()
destroyed: function () {
console.group('destroyed state===============>');
console.log("el : " + this.$el);
console.log(this.$el);
console.log("data : " + this.$data);
console.log("msg: " + this.msg)
}
})
</script>
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
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
# Demo
See the Pen 生命周期钩子 by xugaoyi (@xugaoyi) on CodePen.
# Lifecycle Diagram

Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36