vue所有生命週期函式/鉤子函式理解
下邊是一個template 的程式碼,裡邊包含了所有vue 生命週期函式,我們可以vue-cli 專案中新建一個模板把下邊程式碼全都複製進去,然後F12開啟審查元素,觀察這些生命週期函式列印出來的log 裡的區別!
<template>
<div class="lifecycle">
<p>開啟審查元素檢視console.log 的值, 點選瀏覽器回退按鈕 檢視銷燬函式的執行狀態</p>
<div>{{ message }}</div>
<button @click="handleClick">點選按鈕改變狀態</button>
</div>
</template>
<script>
export default {
data(){
return {
message : "初始值"
}
},
methods: {
handleClick(){
this.message = '被改變的值'
}
},
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)
},
created: function () {
console.group('created 建立完畢狀態===============》');
console.log("%c%s", "color:red","el : " + this.$el); //undefined
console.log("%c%s", "color:red","data : " + this.$data); //已被初始化
console.log("%c%s", "color:red","message: " + this.message); //已被初始化
},
beforeMount: function () {
console.group('beforeMount 掛載前狀態===============》');
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); //已被初始化
},
mounted: function () {
console.group('mounted 掛載結束狀態===============》');
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); //已被初始化
},
beforeUpdate: function () {
console.group('beforeUpdate 更新前狀態===============》');
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);
},
updated: function () {
console.group('updated 更新完成狀態===============》');
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);
},
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>
列印結果如下圖,同學們可以自行操作一番看看
相關文章
- Vue生命週期鉤子函式Vue函式
- Vue 生命週期與鉤子函式Vue函式
- Hive Query生命週期 —— 鉤子(Hook)函式篇HiveHook函式
- Vue父子元件生命週期執行順序及鉤子函式的個人理解Vue元件函式
- Angular2生命週期鉤子函式Angular函式
- Vue的鉤子函式[路由導航守衛、keep-alive、生命週期鉤子]Vue函式路由Keep-Alive
- Vue 生命週期鉤子Vue
- Vue學習(三)生命週期函式Vue函式
- Vue生命週期函式.個人筆記Vue函式筆記
- VUE3 之 生命週期函式Vue函式
- Vue生命週期鉤子簡介Vue
- react的生命週期函式React函式
- Vue例項的生命週期鉤子Vue
- vue生命週期鉤子,一張圖片Vue
- 如何使用 vue + typescript 編寫頁面 (Vue生命週期函式)VueTypeScript函式
- WINDOWS鉤子函式(轉)Windows函式
- VC:滑鼠鉤子函式函式
- Reactv16.8.6生命週期函式React函式
- 對Angular中的生命週期鉤子的理解Angular
- vue 基礎入門筆記 09:生命週期函式Vue筆記函式
- vue[原始碼]生命週期鉤子的實現Vue原始碼
- JVM鉤子函式的使用JVM函式
- JavaScript:鉤子函式與回撥函式的區別JavaScript函式
- ReactNative入門教程-元件生命週期函式React元件函式
- React16 生命週期函式深入淺出React函式
- 小程式的生命週期函式(簡單易懂)函式
- 理解VUE生命週期Vue
- 在vue生命週期裡呼叫函式時犯的一個錯誤Vue函式
- Jetpack Compose(5)——生命週期與副作用函式Jetpack函式
- 鉤子函式的執行機理函式
- before-after-hook鉤子函式Hook函式
- 訊息鉤子函式入門篇函式
- 談談 React 新的生命週期鉤子React
- Vue生命週期的理解Vue
- 如何優雅的消滅掉react生命週期函式React函式
- 訊息鉤子函式入門篇 (轉)函式
- 《前端》VUE學習(二)上(過濾器+鍵盤修飾符+自定義指令+鉤子函式+vue生命週期)--2020年5月25日前端Vue過濾器函式
- Vue的生命週期的理解Vue