Vue 2.0源码略读-- 实例挂载的实现

245 阅读1分钟

实例挂载的实现

new Vue() 执行到到最后,会执行vm.mount(vm.mount(vm.options.el) 函数进行实例挂载,今天我们来看下这个函数的实现过程呢

版本

  1. 运行时 + 编译器(compiler)
  2. 只包含运行时 (src/platform/weex/runtime/index.js)

vue 文档也介绍了这两个版本,我们来看下compiler 版本的实现。 源码在src/platform/web/entry-runtime-with-compiler.js 下:

image.png

先缓存 原型上的$mount 方法,再进行重新定义该方法,会先通过传入 el 参数调用 query(el) 方法返回真实dom节点。

image.png

然后再判断传入的el,是不能挂在body或html这样的根节点上的,因为后面最后挂载的时候会替换掉传入的el;

image.png

接着判断$options参数是否有render 函数,没有的话,会把 el或者template字符串转换生成render函数,

image.png

最后通过compileToFunctions 编译生成render函数,最后只执行原来缓存的mount函数

image.png

$mount函数会去调用mountComponent函数,这个函数定义在src/core/instance/lifecycle.js

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
    // 缓存el
  vm.$el = el
  // vue只认render函数
  if (!vm.$options.render) {
    // 没有render 函数创建空vnode
    vm.$options.render = createEmptyVNode
    if (process.env.NODE_ENV !== 'production') {
      /* istanbul ignore if */
      // 写了template 但用的是runtime-only版本 报警告
      if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') ||
        vm.$options.el || el) {
        warn(
          'You are using the runtime-only build of Vue where the template ' +
          'compiler is not available. Either pre-compile the templates into ' +
          'render functions, or use the compiler-included build.',
          vm
        )
      } else {
        // 没写template ,没写render 报警告
        warn(
          'Failed to mount component: template or render function not defined.',
          vm
        )
      }
    }
  }
  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 && !vm._isDestroyed) {
        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
}

核心是new Watcher 方法,回调中会执行 updateComponent方法 ,在此方法中会执行_render生成VNode,并调用_update方法更新DOM

image.png

主要还是 生成 render 函数