vue 生命週期初探

vue 之後發之勢加上其獨有的特性(壓縮後很小),輕量級的MVVM框架,目前github star已有5.94萬,而react 7萬。因而可知是兩個很是熱門前端框架。這裏就vue的生命週期作個初步體驗。javascript

發現看視頻,動手以後,過段時間仍是會忘,因此寫一篇短文以備不時之需。html

 

先附上官網的圖片:vue生命週期前端

 

 

 

 

生命週期的鉤子函數若是使用得當,會大大增長開發效率:vue

生命週期實踐:java

爲了更好的查看beforeUpdate.updated,beforeDestroy,destroy鉤子函數,使用v-on綁定了點擊事件,setTimeout定時5秒銷燬實例,具體代碼以下:react

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Lifecycle</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p v-on:click="getNew">點我</p>
      <p >{{message}}</p>
    </div>
    <script type="text/javascript">
      var app = new Vue({
        el:'#app',
        data:{
          message:"the running boy"
        },
        methods:{
          getNew:function(){
            this.message = 'I have change';
           }
        },
        beforeCreate:function(){
          console.group("beforeCreate 建立前");
          console.log("%c%s",'color:red','el :' + this.$el);
          console.log("%c%s",'color:red','data :'+ this.$data);
          console.log("%c%s",'color:red','message :' + this.message);
        },
        created:function(){
          console.group("created 建立完畢");
          console.log("%c%s", "color:grey","el : " + (this.$el));
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        beforeMount:function(){
          console.group("beforeMount 掛在以前");
          console.log("%c%s", "color:blue","el : " + (this.$el));
          console.log(this.$el);
          console.log("%c%s", "color:blue","data : " + this.$data);
          console.log("%c%s", "color:blue","message: " + this.message);
        },
        mounted:function(){
          console.group("mounted 掛在結束狀態");
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        beforeUpdate:function(){
          console.group("beforeUpdate 更新狀態以前");
          console.log("%c%s", "color:blue","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:blue","data : " + this.$data);
          console.log("%c%s", "color:blue","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('beforeDestory 銷燬前狀態');
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        destroyed:function(){
          console.group('destroy 銷燬以後');
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        }
      });git

    setTimeout(function(){
      console.log("要銷燬啦");
      app.$destroy();
    },5000);
    /*app.$destroy();*/
  </script>
  </body>
</html>github

 

初次加載以後,查看console,看到以下:前端框架

建立以前,boforeCreate: data 和el 都爲初始化,undefined。服務器

建立以後,created: data初始化,el仍未初始化。

掛載以前,beforeMount: data el 初始化, 另外藍色矩形框內能夠看到,el內仍是{{message}},這裏是應用Virtual DOM 技術,在mounted掛載時再渲染數據。

掛載以後,mounted:  完成掛載。

 

update

點擊頁面頁面「點我」,可獲得以下輸出:

 

藍色方框便是,看也看到點擊事件已執行。

destroy

設置定時5秒銷燬,銷燬以後,點擊事件無效。

 

 

 

這麼多鉤子函數,怎麼使用呢?我也在探索中。。。

beforeCreate: 能夠寫loading事件。

creaded:  在結束loading,還作一些初始化,自執行函數。

mounted:  這裏發起服務器請求。獲得數據,配合路由作一些事情。

beforeDestroy:  刪除組件以前提示,

destroyed:  當前組件已被刪除,清空相關內容

 

本文只寫了部分生命週期的基礎知識,在後續的學習中會陸續更新。

相關文章
相關標籤/搜索