生命周期

生命周期又叫钩子函数,就是在某个节点进行某个操作,这里讲的是构造器里的生命周期,总共有十个,有两个需结合组件学习(activated,deactivated),所以先学八个

avatar


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
<body>
<div id="app">
<p>{{count}}</p>
<button @click="add">增加</button>
</div>
<!-- 注意是onclick不是@click -->
<button onclick="app.$destroy()">销毁</button>
<script>
var app=new Vue({
el:'#app',
data:{
count:1
},
methods:{
add:function(){
this.count++;
}
},
//注意要写在构造器里
beforeCreate:function(){
console.log('1-beforeCreat')
},
created:function(){
console.log('2- created')
},
beforeMount:function(){
console.log('3-beforeMount')
},
mounted:function(){
console.log('4-mounted')
},
beforeUpdate:function(){
console.log('5-beforeUpdate')
},
updated:function(){
console.log('6-updated')
},
beforeDestroy:function(){
console.log('7-beforeDestroy')
},
destroyed:function(){
console.log('8-destroyed')
}
})

</script>
</body>

结果:avatar

-------------本文结束感谢您的阅读-------------