可以總共分為8個(gè)階段:
beforeCreate(創(chuàng)建前)
created(創(chuàng)建后)
beforeMount(載入前)
mounted(載入后)
beforeUpdate(更新前)
updated(更新后)
activated/deactivated
(keep-alive 組件激活時(shí)調(diào)用/keep-alive 組件停用時(shí)調(diào)用)beforeDestroy(銷(xiāo)毀前,不能和keep-alive 在一起使用)
destroyed(銷(xiāo)毀后,不能和keep-alive 在一起使用)
使用場(chǎng)景
1裆悄、Vue-cli 中為單獨(dú)頁(yè)面設(shè)置背景色
只需要給單獨(dú)頁(yè)面添加如下js
created (){
document.body.style.background='#ededed';
},
beforeDestroy(){
document.body.style.background='#f5f5f5';
},
2、demo
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script src="../js/vue.js"></script>
<title></title>
</head>
<body>
<div id='app'>
{{a}}
<button v-on:click="change">change</button>
</div>
<script>
var myVue = new Vue({
el: "#app",
data: {
a: "Vue.js1"
},
methods:{
change:function(){
this.a='Vue.js2'
}
},
beforeCreate: function() {
console.log("創(chuàng)建前")
console.log(this.a)
console.log(this.$el)
},
created: function() {
console.log("創(chuàng)建之后");
console.log(this.a)
console.log(this.$el)
},
beforeMount: function() {
console.log("mount之前")
console.log(this.a)
console.log(this.$el)
},
mounted: function() {
console.log("mount之后")
console.log(this.a)
console.log(this.$el)
},
beforeUpdate: function() {
console.log("更新前");
console.log(this.a)
console.log(this.$el)
},
updated: function() {
console.log("更新完成");
console.log(this.a);
console.log(this.$el)
},
beforeDestroy: function() {
console.log("銷(xiāo)毀前");
console.log(this.a)
console.log(this.$el)
console.log(this.$el)
},
destroyed: function() {
console.log("已銷(xiāo)毀");
console.log(this.a)
console.log(this.$el)
}
})
</script>
</body>
</html>
生成效果圖