vue得生命週期函數探索

原文鏈接:https://blog.csdn.net/qq_15766181/article/details/73549933

轉載於:https://www.cnblogs.com/xiaobaibubai/p/8383952.html

前言:
鉤子就好像是把人的出生到死亡分成一個個階段,你肯定是在出生階段起名字,而不會在成年或者死亡的階段去起名字。或者說你想在出生階段去約炮,也是不行的。組件也是一樣,每個階段它的內部構造是不一樣的。所以一般特定的鉤子做特定的事,比如ajax獲取數據就可以在mounted階段。

一、vue生命週期簡介

咱們從上圖可以很明顯的看出現在vue2.0都包括了哪些生命週期的函數了,總結一下,對官方文檔的那張圖簡化一下,就得到了這張圖。


二、生命週期探究
對於執行順序和什麼時候執行,看上面圖基本有個瞭解了。下面我們將結合代碼去看看鉤子函數的執行。

<!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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章