Da'sBlog

vue-lifecycle-vue生命周期

这里的vue生命周期指的是一个组件创建到销毁的过程。

当组件激活 触发beforeCreate,Create,beforeMount,Mount
当组件不再显示销毁时,触发beforeDestroy,Destroy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div id="app">
<h3>Let's check out the lifecycle of this hur' child.</h3>
<h4>Check the console!</h4>
<button @click="toggleShow">
<span v-if="isShowing">Hide child</span>
<span v-else>Show child</span>
</button>
<hr>
<div v-if="isShowing">
<app-child></app-child>
</div>
</div>
<script type="text/x-template" id="childarea">
<div>
<h4>Here I am!</h4>
</div>
</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
body {
font-family: 'Bitter', serif;
}
#app {
text-align: center;
margin: 60px;
max-width: 320px;
margin: 0 auto;
display: table;
}
.num {
color: #AF007E;
}
button {
font-family: 'Bitter';
background: #c62735;
color: white;
border: 0;
padding: 5px 15px;
margin: 0 10px;
border-radius: 4px;
outline: 0;
cursor: pointer;
}
h4 {
margin: 0 0 15px;
}
hr {
border-color: #F2FAFF;
opacity: 0.5;
margin: 15px 0;
}
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
const Child = {
template: '#childarea',
beforeCreate() {
console.log("beforeCreate!");
},
created() {
console.log("created!");
},
beforeMount() {
console.log("beforeMount!");
},
mounted() {
console.log("mounted!");
},
beforeDestroy() {
console.log("beforeDestroy!");
},
destroyed() {
console.log("destroyed!");
}
};
new Vue({
el: '#app',
data() {
return {
isShowing: false
}
},
methods: {
toggleShow() {
this.isShowing = !this.isShowing;
}
},
components: {
appChild: Child
}
});
坚持原创技术分享,您的支持将鼓励我继续创作!