Vue.js源碼學(xué)習(xí)二 —— 生命周期 LifeCycle 學(xué)習(xí)

春節(jié)繼續(xù)寫博客~加油!

這次來學(xué)習(xí)一下Vue的生命周期,看看生命周期是怎么回事。

callHook

生命周期主要就是在源碼某個時間點執(zhí)行這個 callHook 方法來調(diào)用 vm.$options 的生命周期鉤子方法(如果定義了生命周期鉤子方法的話)。
我們來看看 callHook 代碼:

export function callHook (vm: Component, hook: string) {
  const handlers = vm.$options[hook] // 獲取Vue選項中的生命周期鉤子函數(shù)
  if (handlers) {
    for (let i = 0, j = handlers.length; i < j; i++) {
      try {
        handlers[i].call(vm) // 執(zhí)行生命周期函數(shù)
      } catch (e) {
        handleError(e, vm, `${hook} hook`)
      }
    }
  }
  if (vm._hasHookEvent) {
    vm.$emit('hook:' + hook)
  }
}

比如觸發(fā) mounted 鉤子的方法:

callHook(vm, 'mounted')

生命周期鉤子

先上一張圖看下Vue的生命周期,我們可以在相應(yīng)的生命周期中定義一些事件。


Vue生命周期

beforeCreate & created

先看看這兩個方法調(diào)用的時間。

beforeCreate
在實例初始化之后,數(shù)據(jù)觀測 (data observer) 和 event/watcher 事件配置之前被調(diào)用。
created
在實例創(chuàng)建完成后被立即調(diào)用。在這一步,實例已完成以下的配置:數(shù)據(jù)觀測 (data observer),屬性和方法的運算,watch/event 事件回調(diào)。然而,掛載階段還沒開始,$el 屬性目前不可見。

具體代碼如下

  // src/core/instance/init.js
  Vue.prototype._init = function (options?: Object) {
    ……
    initLifecycle(vm) // 初始化生命周期
    initEvents(vm) // 初始化事件
    initRender(vm) // 初始化渲染
    callHook(vm, 'beforeCreate')
    initInjections(vm) // 初始化Inject
    initState(vm) // 初始化數(shù)據(jù)
    initProvide(vm) // 初始化Provide
    callHook(vm, 'created')
    ……
    if (vm.$options.el) {
      vm.$mount(vm.$options.el) // 如果有el屬性,將內(nèi)容掛載到el中去。
    }
  }

beforeMount & mounted

beforeMount
在掛載開始之前被調(diào)用:相關(guān)的 render 函數(shù)首次被調(diào)用。該鉤子在服務(wù)器端渲染期間不被調(diào)用。
mounted
el 被新創(chuàng)建的 vm.$el 替換,并掛載到實例上去之后調(diào)用該鉤子。如果 root 實例掛載了一個文檔內(nèi)元素,當(dāng) mounted 被調(diào)用時 vm.$el 也在文檔內(nèi)。

貼出代碼邏輯

// src/core/instance/lifecycle.js
// 掛載組件的方法
export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
  vm.$el = el
  if (!vm.$options.render) {
    vm.$options.render = createEmptyVNode
  }
  callHook(vm, 'beforeMount')

  let updateComponent
  updateComponent = () => {
    vm._update(vm._render(), hydrating)
  }
  
  vm._watcher = new Watcher(vm, updateComponent, noop)
  hydrating = false

  if (vm.$vnode == null) {
    vm._isMounted = true
    callHook(vm, 'mounted')
  }
  return vm
}

那么這個 mountComponent 在哪里用了呢?就是在Vue的 $mount 方法中使用。

// src/platforms/web/runtime/index.js
Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && inBrowser ? query(el) : undefined
  return mountComponent(this, el, hydrating)
}

最后會在Vue初始化的時候,判斷是否有 el,如果有則執(zhí)行 $mount 方法。

// src/core/instance/init.js
if (vm.$options.el) {
  vm.$mount(vm.$options.el) // 如果有el屬性,將內(nèi)容掛載到el中去。
}

至此生命周期邏輯應(yīng)該是 beforeCreate - created - beforeMount -mounted

beforeUpdate & updated

beforeUpdate
數(shù)據(jù)更新時調(diào)用,發(fā)生在虛擬 DOM 打補丁之前。這里適合在更新之前訪問現(xiàn)有的 DOM,比如手動移除已添加的事件監(jiān)聽器。
updated
由于數(shù)據(jù)更改導(dǎo)致的虛擬 DOM 重新渲染和打補丁,在這之后會調(diào)用該鉤子。

找代碼邏輯~ beforeUpdate 和 updated 在兩個地方調(diào)用。

Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) {
    const vm: Component = this
    // 如果是已經(jīng)掛載的,就觸發(fā)beforeUpdate方法。
    if (vm._isMounted) {
      callHook(vm, 'beforeUpdate')
    }
    ……
    // updated hook is called by the scheduler to ensure that children are
    // updated in a parent's updated hook.
  }

在執(zhí)行 _update 方法的時候,如果 DOM 已經(jīng)掛載了,則調(diào)用 beforeUpdate 方法。
在 _update 方法的最后作者也注視了調(diào)用 updated hook 的位置:updated 鉤子由 scheduler 調(diào)用來確保子組件在一個父組件的 update 鉤子中。
我們找到 scheduler,發(fā)現(xiàn)有個 callUpdateHooks 方法,該方法遍歷了 watcher 數(shù)組。

// src/core/observer/scheduler.js
function callUpdatedHooks (queue) {
  let i = queue.length
  while (i--) {
    const watcher = queue[i]
    const vm = watcher.vm
    if (vm._watcher === watcher && vm._isMounted) {
      callHook(vm, 'updated')
    }
  }
}

這個 callUpdatedHooksflushSchedulerQueue 方法中調(diào)用。

/**
 * 刷新隊列并運行watcher
 */
function flushSchedulerQueue () {
  flushing = true
  let watcher, id
  queue.sort((a, b) => a.id - b.id)

  for (index = 0; index < queue.length; index++) {
    watcher = queue[index]
    id = watcher.id
    has[id] = null
    watcher.run()
  }

  const activatedQueue = activatedChildren.slice()
  const updatedQueue = queue.slice()

  resetSchedulerState()

  // 調(diào)用組件的updated和activated生命周期
  callActivatedHooks(activatedQueue)
  callUpdatedHooks(updatedQueue)
}

繼續(xù)找下去

export function queueWatcher (watcher: Watcher) {
  const id = watcher.id
  if (has[id] == null) {
    has[id] = true // 此參數(shù)用于判斷watcher的ID是否存在
    ……
    if (!waiting) {
      waiting = true
      nextTick(flushSchedulerQueue)
    }
  }
}

最終在 watcher.js 找到 update 方法:

  // src/core/observer/watcher.js
  update () {
    // lazy 懶加載
    // sync 組件數(shù)據(jù)雙向改變
    if (this.lazy) {
      this.dirty = true
    } else if (this.sync) {
      this.run()
    } else {
      queueWatcher(this) // 排隊watcher
    }
  }

等于是隊列執(zhí)行完 Watcher 數(shù)組的 update 方法后調(diào)用了 updated 鉤子函數(shù)。

beforeDestroy & destroyed

beforeDestroy
實例銷毀之前調(diào)用。在這一步,實例仍然完全可用。該鉤子在服務(wù)器端渲染期間不被調(diào)用。
destroyed
Vue 實例銷毀后調(diào)用。調(diào)用后,Vue 實例指示的所有東西都會解綁定,所有的事件監(jiān)聽器會被移除,所有的子實例也會被銷毀。該鉤子在服務(wù)器端渲染期間不被調(diào)用。

看代碼~

  // src/core/instance/lifecycle.js
  // 銷毀方法
  Vue.prototype.$destroy = function () {
    const vm: Component = this
    if (vm._isBeingDestroyed) {
      // 已經(jīng)被銷毀
      return
    }
    callHook(vm, 'beforeDestroy')
    vm._isBeingDestroyed = true
    // 銷毀過程
    // remove self from parent
    const parent = vm.$parent
    if (parent && !parent._isBeingDestroyed && !vm.$options.abstract) {
      remove(parent.$children, vm)
    }
    // teardown watchers
    if (vm._watcher) {
      vm._watcher.teardown()
    }
    let i = vm._watchers.length
    while (i--) {
      vm._watchers[i].teardown()
    }
    // remove reference from data ob
    // frozen object may not have observer.
    if (vm._data.__ob__) {
      vm._data.__ob__.vmCount--
    }
    // call the last hook...
    vm._isDestroyed = true
    // invoke destroy hooks on current rendered tree
    vm.__patch__(vm._vnode, null)
    // 觸發(fā) destroyed 鉤子
    callHook(vm, 'destroyed')
    // turn off all instance listeners.
    vm.$off()
    // remove __vue__ reference
    if (vm.$el) {
      vm.$el.__vue__ = null
    }
  }

這是一個銷毀 Vue 實例的過程,將各種配置清空和移除。

activated & deactivated

activated
keep-alive 組件激活時調(diào)用。
deactivated
keep-alive 組件停用時調(diào)用。

找到實現(xiàn)代碼的地方

// src/core/instance/lifecycle.js
export function activateChildComponent (vm: Component, direct?: boolean) {
  if (direct) {
    vm._directInactive = false
    if (isInInactiveTree(vm)) {
      return
    }
  } else if (vm._directInactive) {
    return
  }
  if (vm._inactive || vm._inactive === null) {
    vm._inactive = false
    for (let i = 0; i < vm.$children.length; i++) {
      activateChildComponent(vm.$children[i])
    }
    callHook(vm, 'activated')
  }
}

export function deactivateChildComponent (vm: Component, direct?: boolean) {
  if (direct) {
    vm._directInactive = true
    if (isInInactiveTree(vm)) {
      return
    }
  }
  if (!vm._inactive) {
    vm._inactive = true
    for (let i = 0; i < vm.$children.length; i++) {
      deactivateChildComponent(vm.$children[i])
    }
    callHook(vm, 'deactivated')
  }
}

以上兩個方法關(guān)鍵就是修改了 vm._inactive 的值,并且鄉(xiāng)下遍歷子組件,最后觸發(fā)鉤子方法。

errorCaptured

當(dāng)捕獲一個來自子孫組件的錯誤時被調(diào)用。此鉤子會收到三個參數(shù):錯誤對象、發(fā)生錯誤的組件實例以及一個包含錯誤來源信息的字符串。此鉤子可以返回 false 以阻止該錯誤繼續(xù)向上傳播。

這是 2.5 以上版本有的一個鉤子,用于處理錯誤。

// src/core/util/error.js
export function handleError (err: Error, vm: any, info: string) {
  if (vm) {
    let cur = vm
    // 向上冒泡遍歷
    while ((cur = cur.$parent)) {
      // 獲取鉤子函數(shù)
      const hooks = cur.$options.errorCaptured
      if (hooks) {
        for (let i = 0; i < hooks.length; i++) {
          try {
            // 執(zhí)行 errorCaptured 鉤子函數(shù)
            const capture = hooks[i].call(cur, err, vm, info) === false
            if (capture) return
          } catch (e) {
            globalHandleError(e, cur, 'errorCaptured hook')
          }
        }
      }
    }
  }
  globalHandleError(err, vm, info)
}

代碼很簡單,看代碼即可~

生命周期

除了生命周期鉤子外,vue還提供了生命周期方法來直接調(diào)用。

vm.$mount

如果 Vue 實例在實例化時沒有收到 el 選項,則它處于“未掛載”狀態(tài),沒有關(guān)聯(lián)的 DOM 元素??梢允褂?vm.$mount() 手動地掛載一個未掛載的實例。
如果沒有提供 elementOrSelector 參數(shù),模板將被渲染為文檔之外的的元素,并且你必須使用原生 DOM API 把它插入文檔中。
這個方法返回實例自身,因而可以鏈?zhǔn)秸{(diào)用其它實例方法。

const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && query(el)

  if (el === document.body || el === document.documentElement) {
    return this
  }

  const options = this.$options
  // resolve template/el and convert to render function
  if (!options.render) {
    // 獲取template
    let template = options.template
    if (template) {
      if (typeof template === 'string') {
        if (template.charAt(0) === '#') {
          template = idToTemplate(template)
        }
      } else if (template.nodeType) {
        template = template.innerHTML
      } else {
        return this
      }
    } else if (el) {
      template = getOuterHTML(el)
    }
    // 編譯template
    if (template) {
      const { render, staticRenderFns } = compileToFunctions(template, {
        shouldDecodeNewlines,
        shouldDecodeNewlinesForHref,
        delimiters: options.delimiters,
        comments: options.comments
      }, this)
      options.render = render
      options.staticRenderFns = staticRenderFns
    }
  }
  // 執(zhí)行 $mount 方法
  return mount.call(this, el, hydrating)
}

其實很簡單,先獲取html代碼,然后執(zhí)行 compileToFunctions 方法執(zhí)行編譯過程(具體編譯過程在學(xué)習(xí)Render的時候再說)。

vm.$forceUpdate

迫使 Vue 實例重新渲染。注意它僅僅影響實例本身和插入插槽內(nèi)容的子組件,而不是所有子組件。

   Vue.prototype.$forceUpdate = function () {
    var vm = this;
    if (vm._watcher) {
      vm._watcher.update();
    }
  };

這是強制更新方法,執(zhí)行了 vm._watcher.update() 方法。

vm.$nextTick

將回調(diào)延遲到下次 DOM 更新循環(huán)之后執(zhí)行。在修改數(shù)據(jù)之后立即使用它,然后等待 DOM 更新。它跟全局方法 Vue.nextTick 一樣,不同的是回調(diào)的 this 自動綁定到調(diào)用它的實例上。

找了找 vm.$nextTick 的代碼

  // src/core/instance/render.js
  Vue.prototype.$nextTick = function (fn: Function) {
    return nextTick(fn, this)
  }

找到這個 nextTick 方法:

// src/core/util/next-tick.js
export function nextTick (cb?: Function, ctx?: Object) {
  let _resolve
  callbacks.push(() => {
    if (cb) {
      try {
        cb.call(ctx)
      } catch (e) {
        handleError(e, ctx, 'nextTick')
      }
    } else if (_resolve) {
      _resolve(ctx)
    }
  })
  if (!pending) {
    pending = true
    if (useMacroTask) {
      macroTimerFunc()
    } else {
      microTimerFunc()
    }
  }
  // $flow-disable-line
  if (!cb && typeof Promise !== 'undefined') {
    return new Promise(resolve => {
      _resolve = resolve
    })
  }
}

具體功能邏輯等學(xué)習(xí)完 render 再更新……

vm.$destroy

完全銷毀一個實例。清理它與其它實例的連接,解綁它的全部指令及事件監(jiān)聽器。
觸發(fā) beforeDestroy 和 destroyed 的鉤子。

關(guān)于$destroy 我們之前再說 destroyed 鉤子的時候提到過了,這里就不再贅述。

  Vue.prototype.$destroy = function () {
    ……
  }

最后

首先說下過年博客計劃,過年學(xué)習(xí)Vue各個模塊的源碼,并發(fā)布相應(yīng)博客。另外還會發(fā)布一些前端知識的整理,便于下個月找工作~
然后,小結(jié)下自己看源碼的一些小技巧:

  • 重點關(guān)注方法的執(zhí)行、對象的實例化、對象屬性的修改。
  • 忽略開發(fā)版本提示邏輯、內(nèi)部變量賦值。
  • 有目標(biāo)的看代碼,根據(jù)主線目標(biāo)進行源碼學(xué)習(xí)。

OK,今天就這么多~ 明天去學(xué)習(xí)下Vue的事件源碼!加油!明天見!

Vue.js學(xué)習(xí)系列

鑒于前端知識碎片化嚴重,我希望能夠系統(tǒng)化的整理出一套關(guān)于Vue的學(xué)習(xí)系列博客。

Vue.js學(xué)習(xí)系列項目地址

本文源碼已收入到GitHub中,以供參考,當(dāng)然能留下一個star更好啦-。
https://github.com/violetjack/VueStudyDemos

關(guān)于作者

VioletJack,高效學(xué)習(xí)前端工程師,喜歡研究提高效率的方法,也專注于Vue前端相關(guān)知識的學(xué)習(xí)、整理。
歡迎關(guān)注、點贊、評論留言~我將持續(xù)產(chǎn)出Vue相關(guān)優(yōu)質(zhì)內(nèi)容。

新浪微博: http://weibo.com/u/2640909603
掘金:https://gold.xitu.io/user/571d953d39b0570068145cd1
CSDN: http://blog.csdn.net/violetjack0808
簡書: http://www.itdecent.cn/users/54ae4af3a98d/latest_articles
Github: https://github.com/violetjack

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