重学Vue【详说生命周期】

简介: 重学Vue源码,根据黄轶大佬的vue技术揭秘,逐个过一遍,巩固一下vue源码知识点,毕竟嚼碎了才是自己的,所有文章都同步在 公众号(道道里的前端栈) 和 github 上。

网络异常,图片无法展示
|

重学Vue源码,根据黄轶大佬的vue技术揭秘,逐个过一遍,巩固一下vue源码知识点,毕竟嚼碎了才是自己的,所有文章都同步在 公众号(道道里的前端栈) 和 github 上。

网络异常,图片无法展示
|

本篇以上图为例,过一下Vue的生命周期函数,代码在 src/core/instance/lifecycle.js:

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

传进来两个参数:组件类型的 vm 实例 和 钩子函数字符串名称 hook,在上篇的合并配置里面有提到最终合并出来的 vm.$options 的 hook 是一个数组(比如上篇的created),所以这里的 handlers 就是一个数组,数组里每个元素都是一个函数,然后通过 call 把当前上下文vm传入到 handlers 里面,从而页面中的 this 就指向了当前vue的实例。下面来看下都哪些地方执行了这个 callHook。


beforeCreate 和 created

首先是init初始化过程:

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

可以看到调用了两次 callHook,一个是 beforeCreated,一个是 created,这两个的区别就在于执行 beforeCreated 的时候,只进行了初始化生命周期,事件和渲染,此时还拿不到数据,而 created 是在初始化注入,数据和provider之后走的,所以可以拿到数据。


beforeMount 和 mounted

之前提到在执行挂载的时候会执行一个 mountComponent 方法,它里面有 vm._update(vm._render(),hydrating) 和 Watcher 等等,再来看下这个方法:

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 name = vm._name
      const id = vm._uid
      const startTag = `vue-perf-start:${id}`
      const endTag = `vue-perf-end:${id}`
      mark(startTag)
      const vnode = vm._render()
      mark(endTag)
      measure(`vue ${name} render`, startTag, endTag)
      mark(startTag)
      vm._update(vnode, hydrating)
      mark(endTag)
      measure(`vue ${name} patch`, startTag, endTag)
    }
  } 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 */)
  hydrating = false
  // manually mounted instance, call mounted on self
  // mounted is called for render-created child components in its inserted hook
  if (vm.$vnode == null) {
    vm._isMounted = true
    callHook(vm, 'mounted')
  }
  return vm
}

它在执行 vm._render() 渲染VNode之前,执行了一个 beforeMount 钩子,在执行完 new Watcher 里有个逻辑,如果 vm.$vnode 是空,也就是当前vm没有父节点,也就意味着当前节点就是根节点,就执行一次 mounted,这个是根组件执行的,那在子组件创建的时候,内部也有patch过程,这个过程在最后调用了一个 invokeInsertHook():

export function createPatchFunction (backend) {
  // ...
  return function patch (oldVnode, vnode, hydrating, removeOnly) {
   // ...
    invokeInsertHook(vnode, insertedVnodeQueue, isInitialPatch)
    return vnode.elm
  }
}

看下它的定义:

function invokeInsertHook (vnode, queue, initial) {
  // delay insert hooks for component root nodes, invoke them after the
  // element is really inserted
  if (isTrue(initial) && isDef(vnode.parent)) {
    vnode.parent.data.pendingInsert = queue
  } else {
    for (let i = 0; i < queue.length; ++i) {
      queue[i].data.hook.insert(queue[i])
    }
  }
}

这个函数会执行 insert 钩子,那对于组件而言,这个 insert 定义在 createVNodeHooks 里面,也就是之前文章中说到的创建子组件过程中的安装组件钩子那一步:

const componentVNodeHooks = {
  // ...
  insert (vnode: MountedComponentVNode) {
    const { context, componentInstance } = vnode
    if (!componentInstance._isMounted) {
      componentInstance._isMounted = true
      callHook(componentInstance, 'mounted')
    }
    // ...
  },
}

这里可以看到如果子组件没有自定义 mounted,就把子组件 call 一下,加上 mounted 钩子,也就是说每一个子组件都是在这个 insert 执行了 mounted,而且之前分析过子组件的patch过程,先init,然后如果返回的组件根vnode是一个组件,就重复执行patch的init,在这个过程中上面提到的 queue 也是在不断的往里面添加钩子,而子组件的vnode总是优先插到队列里,所以在全部的patch结束,去调用钩子函数的时候,子组件的 insert 钩子就会先于父组件执行,也就是说子组件的 mounted 会先执行,父组件的 mounted 会后执行。

对于 beforeMount 而言,则是父优先于子,因为父组件的 mountComponent 会优先于子的执行,然后才会执行子组件的patch,里面会调用子组件的初始化,而子组件初始化的时候又会调用子组件的 mountComponent,所以是先父后子。


beforeUpdate 和 updated

在组件执行 mountComponent 的时候会执行一个 new Watcher,里面有一个 before 函数它调用了 beforeUpdate 钩子:

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
  // ...
  new Watcher(vm, updateComponent, noop, {
    before () {
      if (vm._isMounted) {
        callHook(vm, 'beforeUpdate')
      }
    }
  }, true /* isRenderWatcher */)
  // ...
}

这里有个判断,只有组件在执行 mounted 之后,才会调用 beforeUpdate 钩子,也就是说 beforeUpdate 和 updated 都是在 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')
    }
  }
}

flushSchedulerQueue 的 updatedQueue 参数是更新了的 watcher 数组,然后在调用 callUpdatedHooks 的时候,做了一个判断,只有当前的 watcher 是 vm._watcher 并且组件已经执行完毕 mounted,也就是mounted过了并且数据发生变化了,才会执行 update 钩子。而 vm._watcher === watcher 这个判断是从哪里来的?

前面提到组件 mounted 过程中,会实例化一个渲染 Watcher 去监听 vm 上的数据变化:

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
  // ...
  // 这里是简写
  let updateComponent = () => {
      vm._update(vm._render(), hydrating)
  }
  new Watcher(vm, updateComponent, noop, {
    before () {
      if (vm._isMounted) {
        callHook(vm, 'beforeUpdate')
      }
    }
  }, true /* isRenderWatcher */)
  // ...
}

而在实例化这个渲染 Watcher 过程中,会判断一个 isRenderWatcher,Watcher 的构造函数如下,定义在 src/core/observer/watcher.js 中:

export default class Watcher {
  // ...
  constructor (
    vm: Component,
    expOrFn: string | Function,
    cb: Function,
    options?: ?Object,
    isRenderWatcher?: boolean
  ) {
    this.vm = vm
    if (isRenderWatcher) {
      vm._watcher = this
    }
    vm._watchers.push(this)
    // ...
  }
}

这里的 vm._watcher 是专门用来监听 vm 上数据变化然后重新渲染的,所以 vm._watcher 是一个渲染 watcher,接着判断如果当前 watcher 是一个渲染 watcher,那就把这个渲染 watcher 赋值给 vm._watcher,并且把它,也就是当前的 watcher 实例push到 vm._watchers 中,所以在 callUpdatedHooks 里的 vm._watcher === watcher 的意思就是当前 watcher 是一个渲染 watcher 的时候并且mounted之后,这个渲染 watcher 才会执行 updated 钩子。

vm._isMounted 是定义在 insert 里:

insert (vnode: MountedComponentVNode) {
  const { context, componentInstance } = vnode
  if (!componentInstance._isMounted) {
    componentInstance._isMounted = true
    callHook(componentInstance, 'mounted')
  }
  // ...
}

首次渲染的话, componentInstance._isMounted 是false,所以只会执行一个 mounted,当后面重新渲染的时候,这个 _isMounted 就是true了,就会跳过这里,去执行 updated。(重新渲染在响应式原理文章里会提到)


beforeDestroy 和 destroyed

这两个钩子执行是在 $destroy 的时候:

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,然后进行了一系列的销毁工作,销毁完成之后会执行 destroyed,这里注意有一个:vm.__patch__(vm._vnode, null),第二个参数是null,因为每一个组件都是一个vue实例,当前组件执行 $destroy 的时候,就会递归销毁子组件。

所以对于 beforeDestroy 过程是先父后子,也就是说父组件先执行 $destroy ,然后在patch过程中,走子组件的 $destroy ,这样在执行 destroyed 的时候就是先子后父了,因为patch过程会递归把最内层子组件优先执行。

目录
相关文章
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
1072 2
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
932 137
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
2243 0
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
980 17
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
1128 1
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
1105 1
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
853 0
|
存储 JavaScript 前端开发
如何高效实现 vue 文件批量下载及相关操作技巧
在Vue项目中,实现文件批量下载是常见需求。例如文档管理系统或图片库应用中,用户可能需要一次性下载多个文件。本文介绍了三种技术方案:1) 使用`file-saver`和`jszip`插件在前端打包文件为ZIP并下载;2) 借助后端接口完成文件压缩与传输;3) 使用`StreamSaver`解决大文件下载问题。同时,通过在线教育平台的实例详细说明了前后端的具体实现步骤,帮助开发者根据项目需求选择合适方案。
1322 0
|
JavaScript 前端开发 UED
Vue 项目中如何自定义实用的进度条组件
本文介绍了如何使用Vue.js创建一个灵活多样的自定义进度条组件。该组件可接受进度段数据数组作为输入,动态渲染进度段,支持动画效果和内容展示。当进度超出总长时,超出部分将以红色填充。文章详细描述了组件的设计目标、实现步骤(包括props定义、宽度计算、模板渲染、动画处理及超出部分的显示),并提供了使用示例。通过此组件,开发者可根据项目需求灵活展示进度情况,优化用户体验。资源地址:[https://pan.quark.cn/s/35324205c62b](https://pan.quark.cn/s/35324205c62b)。
776 0