Vue#1.0和#2.0生命周期詳解
-
1.0 生命周期
image- 2.0 生命周期
imageimage
兩個版本對比
image
vue的生命周期各階段都做了什么?
beforeCreate 實例創建前:這個階段實例的data、methods是讀不到的
created 實例創建后:這個階段已經完成了數據觀測(dataobserver),屬性和方法的運算,watch/event 事件回調。mount掛載階段還沒開始,$el屬性目前不可見,數據并沒有在DOM元素上進行渲染
beforeMount:在掛載開始之前被調用:相關的 render 函數首次被調用。
mounted:el選項的DOM節點被新創建的vm.$el替換,并掛載到實例上去之后調用此生命周期函數。此時實例的數據在DOM節點上進行渲染
beforeUpdate:數據更新時調用,但不進行DOM重新渲染,在數據更新時DOM沒渲染前可以在這個生命函數里進行狀態處理
updated:這個狀態下數據更新并且DOM重新渲染,當這個生命周期函數被調用時,組件 DOM 已經更新,所以你現在可以執行依賴于 DOM 的操作。當實例每次進行數據更新時updated都會執行
beforeDestory:實例銷毀之前調用。
destroyed:Vue 實例銷毀后調用。調用后,Vue 實例指示的所有東西都會解綁定,所有的事件監聽器會被移除,所有的子實例也會被銷毀。
<!DOCTYPE html>
<html>
<head>
<title>鉤子函數</title>
<script type="text/javascript" src="vue_2.2.4.js"></script>
<body>
<div id="app">
<p>{{ message }}</p>
<input type="button" @click="change" value="更新數據" />
<input type="button" @click="destroy" value="銷毀" />
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
message : "Welcome Vue"
},
methods:{
change() {
this.message = 'Datura is me';
},
destroy() {
vm.$destroy();
}
},
beforeCreate: function () {
console.group('beforeCreate 創建前狀態===============》');
console.log("%c%s", "color:red","el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //undefined
console.log("%c%s", "color:red","message: " + this.message);//undefined
},
created: function () {
console.group('created 創建完畢狀態===============》');
console.log("%c%s", "color:red","el : " + this.$el); //undefined
console.log("%c%s", "color:green","data : " + this.$data); //[object Object] => 已被初始化
console.log("%c%s", "color:green","message: " + this.message); //Welcome Vue => 已被初始化
},
beforeMount: function () {
console.group('beforeMount 掛載前狀態===============》');
console.log("%c%s", "color:green","el : " + (this.$el)); //已被初始化
console.log(this.$el); // 當前掛在的元素
console.log("%c%s", "color:green","data : " + this.$data); //已被初始化
console.log("%c%s", "color:green","message: " + this.message); //已被初始化
},
mounted: function () {
console.group('mounted 掛載結束狀態===============》');
console.log("%c%s", "color:green","el : " + this.$el); //已被初始化
console.log(this.$el);
console.log("%c%s", "color:green","data : " + this.$data); //已被初始化
console.log("%c%s", "color:green","message: " + this.message); //已被初始化
},
beforeUpdate: function () {
alert("更新前狀態");
console.group('beforeUpdate 更新前狀態===============》'); //這里指的是頁面渲染新數據之前
console.log("%c%s", "color:green","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:green","data : " + this.$data);
console.log("%c%s", "color:green","message: " + this.message);
alert("更新前狀態2");
},
updated: function () {
console.group('updated 更新完成狀態===============》');
console.log("%c%s", "color:green","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:green","data : " + this.$data);
console.log("%c%s", "color:green","message: " + this.message);
},
beforeDestroy: function () {
console.group('beforeDestroy 銷毀前狀態===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
destroyed: function () {
console.group('destroyed 銷毀完成狀態===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message)
}
})
</script>
</body>
</html>