vue生命周期簡介和鈎子函數


轉載:http://blog.csdn.net/qq_15766181/article/details/73549933

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

一、vue生命周期簡介


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

二、生命周期探究

對於執行順序和什么時候執行,看上面圖基本有個了解了。下面我們將結合代碼去看看鈎子函數的執行。

<!DOCTYPE html>
<html>
<head>
    <title>鈎子函數</title>
    <meta charset="utf-8">
    <script src="http://cdn.bootcss.com/vue/2.1.10/vue.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>

 

 
1. create 和 mounted

beforecreated:el 和 data 並未初始化
created:完成了 data 數據的初始化,el沒有
beforeMount:完成了 el 和 data 初始化
mounted :完成掛載

另外在標紅處,我們能發現el還是 {{message}},這里就是應用的 Virtual DOM(虛擬Dom)技術,先把坑占住了。到后面mounted掛載的時候再把值渲染進去。

2. update

我們單擊頁面中的“更新數據”按鈕,將數據更新。下面就能看到data里的值被修改后,將會觸發update的操作。

ps:注意beforeUpdate是指view層的數據變化前,不是data中的數據改變前觸發。因為Vue是數據驅動的。注意觀察彈窗就容易發現。
3. destroy

銷毀完成后,我們再重新改變message的值,vue不再對此動作進行響應了。但是原先生成的dom元素還存在,可以這么理解,執行了destroy操作,后續就不再受vue控制了。因為這個Vue實例已經不存在了。
我們單擊頁面中的“銷毀”按鈕,將指定的Vue實例銷毀。

三、生命周期總結

beforecreate : 舉個栗子:可以在這加個loading事件
created :在這結束loading,還做一些初始化,實現函數自執行
mounted : 在這發起后端請求,拿回數據,配合路由鈎子做一些事情
beforeDestory: 你確認刪除XX嗎? destoryed :當前組件已被刪除,清空相關內容


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM