vue.js入門(六,生命周期)

生命周期是貫穿Vue項目的一個重要知識點(想更多了解的建議搜索類似的掘金文章,入門文章)

創(chuàng)建階段(流程如下)

(初始化事件和生命周期)beforeCreate(數(shù)據(jù)觀測、屬性、偵聽器配置等)——>created——>(模板編譯到render)beforeMount——>render(掛載到DOM)——>mounted(異步請求、操作DOM、定時器等)

更新階段(多次執(zhí)行的階段,$forceUpdate強制更新也會觸發(fā))

beforeUpdate——>render——>updated(更新代碼中不能修改數(shù)據(jù),不然會死循環(huán))

銷毀階段

beforeDestory——>destoryed

DEMO

<template>
  <div>
    {{name}}
    <button @click="desc">修改</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      name:'Vue'
    };
  },
  beforeCreate() {
    console.log("beforeCreate");
  },
  created() {
    console.log("created");
  },
  beforeMount() {
    console.log("beforeMount");
  },
  mounted() {
    console.log("mounted");
  },
  beforeUpdate() {
    console.log("beforeUpdate");
  },
  updated() {
    console.log("updated");
  },
  beforeDestroy() {
    console.log("beforeDestroy");
  },
  destroyed() {
    console.log("destroyed");
  },
  methods:{
    desc(){
      this.name='123'
    }
  }
};
</script>

因為對象是無序的,位置在哪無所謂

接著觸發(fā)更新,調(diào)用了更新函數(shù)

如果銷毀組件的話,就不會發(fā)生更新了

  methods:{
    desc(){
      this.$destroy()
      this.name='123'
    }
  }

name更新失敗
(先這樣,后面可能寫其他DEMO的時候會寫到深入一些)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容