2.8 组件的生命周期
实例化Vue对家
newVue0
Ilnit
初始化重件和生命店期
Events&Lifecycle
创建实例之前热行的钢子工件
beforeCreate
Init
切始化注入
Injections&reactivlty
实到创亚完成后热行的钓子
created
Has
NO
有设有e选项
elr'option?
没有则使用vm.Smount0去挂数模板
Vm.Smount(el)
YES
iscalled
Has
template"optlon?
NO
给译共极
YES
iEdata对乐里的数据
Complletemplate
Complleel's
twue语法声明的快板
outerHTML
译蔗询览整可法的HTMLout
renderfunction
astemplate
将缤注完成的HTML抖我到
对应虚抖dom时钟发的钞子
beforeMount
此时页面并没有内容
CreatevmSel
将纷译务的HTML专奖荣e一性
andreplace
所器向的dom
erwithit
更新之前约的子
mounted
译好的HTML挂金到页百完成信执行的土件约子
Whendata
changes
此钓子画欲中一根会做一实小2x计求获段数据
进行发活切始化
实时蓝控数指变化
注意:mounted在整个实例中只扰行一次
开阶之更斯DOM
VirtualDOm
Mounted
re-render
andpatch
When
实例销袋
Vm.sdestroy0
updated
iscalled
实例省毁之前执行的的子
史新之后的针于
beforeDestroy
Teardown
拆除数据监听,子组件和有件篮听
watchers.chlld
componentsand
eventlisteners
实圳铺毁完成执行的筒子
Destroyed
destroyed
TemplAteCompilationisperforMdaheaDoftimefusing
abuildstep.eg.single-flecomponents
Vue中的组件也是有生命周期的。一个Vue组件会经历:初始化、创建、绑定、更新、销毁等阶段,不同的阶段,都会有相应的生命周期钩子函数被调用。
HTML
复制代码
1
<html>2
<head>3
<meta charset="UTF-8">4
<title>生命周期</title>5
</head>6
<body>7
<div id="app1">8
{{title}}9
<button type="button" @click="changeTitle">change title</button>10
<button type="button" @click="destroy">destroy</button>11
</div>12
</body>13
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>14
<script>15
new Vue({16
el:"#app1",17
data:{18
title:"this is title"19
},20
methods:{21
changeTitle:function(){22
this.title= "new title";23
},24
destroy:function(){25
this.$destroy();26
}27
},28
beforeCreate(){29
console.log("beforeCreate")30
},31
created(){32
console.log("created")33
},34
beforeMount(){35
console.log("beforeMount")36
},37
mounted(){38
console.log("mounted")39
},40
beforeUpdate(){41
console.log("beforeUpdate")42
},43
updated(){44
console.log("updated")45
},46
beforeDestroy(){47
console.log("beforeDestory")48
},49
destroyed(){50
console.log("destory")51
}52
})53
</script>54
</html>本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。