【Vue源码】第十三节组件化之认识生命周期

在这里插入图片描述
源码中最终执行生命周期的函数都是调用 callHook 方法,它的定义在 src/core/instance/lifecycle中:

export function callHook (vm: Component, hook: string) {
    
    
  // #7573 disable dep collection when invoking lifecycle hooks
  pushTarget()
  const handlers = vm.$options[hook]
  if (handlers) {
    
    
    for (let i = 0, j = handlers.length; i < j; i++) {
    
    
      try {
    
    
        handlers[i].call(vm)
      } catch (e) {
    
    
        handleError(e, vm, `${
      
      hook} hook`)
      }
    }
  }
  if (vm._hasHookEvent) {
    
    
    vm.$emit('hook:' + hook)
  }
  popTarget()
}

callHook 函数的逻辑很简单,根据传入的字符串 hook,去拿到 vm.$options[hook] 对应的回调函数数组,然后遍历执行,执行的时候把 vm 作为函数执行的上下文。

了解了生命周期的执行方式后,接下来我们会具体介绍每一个生命周期函数它的调用时机。

beforeCreate & created

beforeCreatecreated 函数都是在实例化 Vue 的阶段,在 _init 方法中执行的,它的定义在 src/core/instance/init.js 中:

Vue.prototype._init = function (options?: Object) {
    
    
  // ...
  initLifecycle(vm)
  initEvents(vm)
  initRender(vm)
  callHook(vm, 'beforeCreate')
  initInjections(vm) // resolve injections before data/props
  initState(vm)
  initProvide(vm) // resolve provide after data/props
  callHook(vm, 'created')
  // ...
}

根据以前的知识我们知道initState 的作用是初始化 propsdatamethodswatchcomputed 等属性,所以在beforeCreate时我们就不能获取到这些值,而created可以。

另外,这两个钩子执行时,也没有渲染DOM,所以不能访问DOM。

beforeMount & mounted

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
    
    
  vm.$el = el
  // ...
  callHook(vm, 'beforeMount')

  let updateComponent
  /* istanbul ignore if */
  if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
    
    
    updateComponent = () => {
    
    
      // ...
      const vnode = vm._render()
      // ...
      mark(startTag)
      vm._update(vnode, hydrating)
      // ...
    }
  } else {
    
    
    updateComponent = () => {
    
    
      vm._update(vm._render(), hydrating)
    }
  }

  // we set this to vm._watcher inside the watcher's constructor
  // since the watcher's initial patch may call $forceUpdate (e.g. inside child
  // component's mounted hook), which relies on vm._watcher being already defined
  new Watcher(vm, updateComponent, noop, {
    
    
    before () {
    
    
      if (vm._isMounted) {
    
    
        callHook(vm, 'beforeUpdate')
      }
    }
  }, true /* isRenderWatcher */)

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

beforeMount钩子发生在_render之前,在执行完_update之后,才会执行mounted钩子,这个是针对new Vue过程的。而对于子组件而言,每次Vue将子组件的VNode patch到DOM之后,都会执行invokeInsertHook函数,在这个函数里面会执行insert钩子函数,就会调用子组件的mounted钩子。

由于我们子组件的挂载顺序是先子后父,所以mounted的执行顺序也是。

beforeUpdate & updated

beforeUpdateupdated 的钩子函数执行时机都应该是在数据更新的时候。

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
    
    
  // ...

  // we set this to vm._watcher inside the watcher's constructor
  // since the watcher's initial patch may call $forceUpdate (e.g. inside child
  // component's mounted hook), which relies on vm._watcher being already defined
  new Watcher(vm, updateComponent, noop, {
    
    
    before () {
    
    
      if (vm._isMounted) {
    
    
        callHook(vm, 'beforeUpdate')
      }
    }
  }, true /* isRenderWatcher */)
  // ...
}

注意这里有个判断,也就是在组件已经 mounted 之后,才会去调用这个钩子函数。

update 的执行时机是在flushSchedulerQueue 函数调用的时候,它的定义在 src/core/observer/scheduler.js 中:

function flushSchedulerQueue () {
    
    
  // ...
  // 获取到 updatedQueue
  callUpdatedHooks(updatedQueue)
}

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')
    }
  }
}

updatedQueue 是更新了的 wathcer 数组,那么在 callUpdatedHooks 函数中,它对这些数组做遍历,只有满足当前 watchervm._watcher 以及组件已经 mounted 这两个条件,才会执行 updated 钩子函数。

beforeDestroy & destroyed

beforeDestroydestroyed 钩子函数的执行时机在组件销毁的阶段,组件的销毁过程之后会详细介绍,最终会调用 $destroy 方法,它的定义在 src/core/instance/lifecycle.js 中:

Vue.prototype.$destroy = function () {
    
    
    const vm: Component = this
    if (vm._isBeingDestroyed) {
    
    
      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)
    // fire destroyed hook
    callHook(vm, 'destroyed')
    // turn off all instance listeners.
    vm.$off()
    // remove __vue__ reference
    if (vm.$el) {
    
    
      vm.$el.__vue__ = null
    }
    // release circular reference (#6759)
    if (vm.$vnode) {
    
    
      vm.$vnode.parent = null
    }
  }

beforeDestroy 钩子函数的执行时机是在 $destroy 函数执行最开始的地方,接着执行了一系列的销毁动作,包括从 parent$children 中删掉自身,删除 watcher,当前渲染的 VNode 执行销毁钩子函数等,执行完毕后再调用 destroy 钩子函数。

$destroy 的执行过程中,它又会执行 vm.__patch__(vm._vnode, null) 触发它子组件的销毁钩子函数,这样一层层的递归调用,所以 destroy 钩子函数执行顺序是先子后父,和 mounted 过程一样。

猜你喜欢

转载自blog.csdn.net/qq_34086980/article/details/105934084