生命周期三個(gè)階段:初始化階段售担,更新階段嘶摊,死亡階段
生命周期回調(diào)函數(shù):mounted 做異步任務(wù)铅匹,發(fā)送ajax請(qǐng)求, beforeDestory 做收尾工作清除定時(shí)器等每辟。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="demo">
<button @click="destoryVM">destory vm</button>
<p v-show="isShow">沈聰小仙女</p>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
new Vue({
el:'#demo',
data:{
isShow:true
},
// 1.初始化階段
beforeCreate(){
console.log('beforeCreate()');
},
created(){
console.log('created()');
},
beforeMount(){
console.log('beforeMounted()');
},
mounted(){ //初始化顯示之后立即調(diào)用1次
this.intervalId = setInterval(() => {
console.log('1');
this.isShow = !this.isShow; //更新數(shù)據(jù)
},1000)
},
// 2.更新階段
beforeUpdate(){
console.log('beforeUpdate()');
},
updated(){
console.log('update()');
},
//3.死亡階段
beforeDestroy(){ //死亡之前調(diào)用1次
//清除定時(shí)器
clearInterval(this.intervalId);
},
methods:{
destoryVM(){
//干掉VM
this.$destroy();
}
}
})
</script>
</html>