This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
06-lifecycle-hooks.html
86 lines (77 loc) · 2.06 KB
/
06-lifecycle-hooks.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!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></title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<style>
body {
font-size: 1.5em;
}
#app {
margin-bottom: 1em;
}
</style>
</head>
<div id="app">
<h1>{{ count }}</h1>
<button @click="updateEvent">Update</button>
</div>
<button id="del">Del</button>
<script>
var vm = new Vue({
el: '#app',
data: {
count: 0
},
methods: {
updateEvent: function() {
this.count += 1;
}
},
beforeCreate: function() {
// 元件實體剛被建立,屬性計算之前。
console.log('beforeCreate - this.count: ', this.count);
console.log('beforeCreate - this.$el: ', this.$el);
},
created: function() {
// 元件實體已建立,屬性已綁定,但 DOM 還沒生成。
console.log('created - this.count: ', this.count);
console.log('created - this.$el: ', this.$el);
},
beforeMount: function() {
// 模板 (template) 編譯或掛載至 HTML 之前
console.log('beforeMount - this.$el: ', this.$el);
},
mounted: function() {
// 模板 (template) 編譯或掛載至 HTML 之後
console.log('mounted - this.$el: ', this.$el);
},
beforeUpdate: function() {
// 元件被更新之前
console.log('beforeUpdate: ',
this.$el.querySelector('h1').innerText,
this.count);
},
updated: function() {
// 元件被更新之後
console.log('updated: ',
this.$el.querySelector('h1').innerText,
this.count);
},
beforeDestroy: function() {
// 移除 vue instance 之前
console.log('beforeDestroy');
},
destroyed: function() {
// 移除 vue instance 之後
console.log('destroyed');
}
});
document.getElementById('del').addEventListener('click', function() {
vm.$destroy();
});
</script>
</html>