Vue2.0基礎(chǔ)-生命周期(二)

一、Vue對象實例化

JS代碼:

new Vue({
  /* DOM掛載點 */
  el: '#app',
  /* 使用哪個模板 */
  template: '<div>{{fruit}}</div>',
  /* 給模板傳遞的數(shù)據(jù) */
  data: {
    fruit: 'apple'
  }
})

其中Vue函數(shù)稱為構(gòu)造函數(shù),使用new就可以實例化出來一個實例化對象。

二、Vue的生命周期

剛剛接觸Vue.js, 之前使用React.js知道需要搞清楚它的生命周期及其每個鉤子函數(shù)的含義。

Vue生命周期.png
鉤子函數(shù) Description
beforeCreate 組件實例剛被創(chuàng)建,組件屬性計算之前,如data屬性等
created 組件實例創(chuàng)建完成,屬性已綁定,但DOM還未生成,$el屬性還不存在
beforeMount 模板編譯/掛載之前
mounted 模板編譯/掛載之后
beforeUpdate 組件更新之前
updated 組件更新之后
activated for keep-alive, 組件被激活時調(diào)用
deactivated for keep-alive, 組件被移除時調(diào)用
beforeDestory 組件銷毀前調(diào)用

2.1生命周期的探究

對于執(zhí)行順序和什么時間執(zhí)行,看上面的圖會比較沒有太多的耐心,因此先看一下鉤子函數(shù)的執(zhí)行然后再回頭看圖會更容易理解。

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
var app1 = new Vue({
  el: '#app',
  router,
  data: {
    message: 'kuaixiang'
  },
  beforeCreate: function () {
    console.group('beforeCreate 創(chuàng)建之前:')
    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 創(chuàng)建完畢:')
    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)
  },
  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 掛載結(jié)束:')
    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.group('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.group('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.group('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)
  },
  destroyed: function () {
    console.group('destroyed 銷毀完成:')
    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)
  },
  template: '<App/>',
  components: { App }
})

console.log(app1)

這段代碼驗證了再Vue.js中的生命周期的每個階段,需要再結(jié)合圖來看每個鉤子函數(shù)。

參考文獻

https://segmentfault.com/a/1190000008010666

最后編輯于
?著作權(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)容