Vue2.0關於生命周期和鈎子函數


Vue生命周期簡介:


 

 

Vue1.0+和Vue2.0在生命周期鈎子上的區別還是很大的,如下:

 


 

代碼驗證:

<!DOCTYPE html>

<html>

     <head>

        <meta charset="utf-8">

        <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:"Toney is a girl"

            },

            beforeCreate:function(){

                 console.group('beforeCreat  創建前的狀態======》');  //控制台輸出的語句產生不同的層級嵌套關系

                console.log("%c%s","color:red","el : "+this.$el);  //undefined,  %c字符%s字符串

                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: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("%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("%c%s","color:red","data : "+this.$data");  //已被初始化

              console.log("%c%s","color:red","message: "+this.message);  //已被初始化

           },

          beforeUpdate:function(){

              console.log("beforeUpdate 更新前狀態======》");

              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);

          },

          updated:function(){

             console.log("updated  更新完成狀態======》");

             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);

          },

          beforeDestory:function(){

             console.log("beforeDestory 銷毀前狀態======》");

             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);

          },

          destoryed:function(){

              console.log("destoryed 銷毀完成狀態======》");

              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);

          }

        })

      </script>

  </body> 


 

關於更新

在chrome console中輸入命令:

 

app.message="I am a girl"

 


 

關於銷毀

在chrome console中輸入命令:

app.$destroy();

 


 

生命周期總結:

beforecreate: 例子:可以在這加個loading事件;

created:在這結束loading,還做一些初始化,實現函數自執行;

mounted: 在這發起后端請求,拿回數據,配合路由鈎子做一些事情;

beforeDestory: 你確認刪除XX嗎? destoryed :當前組件已被刪除,清空相關內容。

 


免責聲明!

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



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