vue 生命週期

 

 vue 生命週期圖

 

 實驗

 
 
<section id="app-8"> {{data}} </section>

var
myVue=new Vue({ el:"#app-8", data:{ data:"aaaaa", info:"nono" }, beforeCreate:function(){ console.log("建立前========") console.log(this.data) console.log(this.$el) }, created:function(){ console.log("已建立========") console.log(this.info) console.log(this.$el) }, beforeMount:function(){ console.log("mount以前========") console.log(this.info) console.log(this.$el) }, mounted:function(){ console.log("mounted========") console.log(this.info) console.log(this.$el) }, beforeUpdate:function(){ console.log("更新前========"); }, updated:function(){ console.log("更新完成========"); }, beforeDestroy:function(){ console.log("銷燬前========") console.log(this.info) console.log(this.$el) }, destroyed:function(){ console.log("已銷燬========") console.log(this.info) console.log(this.$el) } })

 

------

知識點

由上圖可知:

  一、beforeCreate 此時$el、data 的值都爲undefinedhtml

  二、建立以後,此時能夠拿到data的值,可是$el依舊爲undefinedvue

  三、mount以前,$el的值爲「虛擬」的元素節點app

  四、mount以後,mounted以前,「虛擬」的dom節點被真實的dom節點替換,並將其插入到dom樹中,因而在觸發mounted時,能夠獲取到$el爲真實的dom元素()dom

  myVue.$el===document.getElementById("app-8")  // truethis

 

接着,在console中修改data,更新視圖spa

 

 

   觸發beforeUpdata 和updatedcode

 

  接着,執行myVue.$destroy()htm

 

 總結一下,對官方文檔的那張圖簡化一下,就獲得了這張圖

 

 文章中如有錯誤請指出,轉載請註明出處,謝謝~blog

 原文
原文
https://www.cnblogs.com/gagag/p/6246493.html
相關文章
相關標籤/搜索