1. 程式人生 > >vue生命周期和鉤子函數的理解

vue生命周期和鉤子函數的理解

for body undefine div doctype 操作 sage 函數 ivr

vue生命周期簡介

            技術分享圖片

技術分享圖片

生命周期探究

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

  

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>
<body>

  <div id="app">

    <p>{{ message }}</p>
  </div>

  <script type="text/javascript">

    var app = new Vue({
  el: ‘#app‘,
    data: {
      message : "xuxiao is boy"
    },
    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>
</body>
</html>

create 和 mounted 相關

chrome瀏覽器裏打開,F12console就能發現

技術分享圖片

技術分享圖片

update 相關

這裏我們在 chrome console裏執行以下命令

技術分享圖片

下面就能看到data裏的值被修改後,將會觸發update的操作

技術分享圖片

destroy 相關

有關於銷毀,暫時還不是很清楚。我們在console裏執行下命令對 vue實例進行銷毀。銷毀完成後,我們再重新改變message的值,vue不再對此動作進行響應了。但是原先生成的dom元素還存在,可以這麽理解,執行了destroy操作,後續就不再受vue控制了。

技術分享圖片

技術分享圖片

生命周期總結

技術分享圖片

vue生命周期和鉤子函數的理解