关于vue.js:Vue组件是怎样挂载的

5次阅读

共计 5113 个字符,预计需要花费 13 分钟才能阅读完成。

咱们先来关注一下 $mount 是实现什么性能的吧:

咱们关上源码门路core/instance/init.js:

export function initMixin (Vue: Class<Component>) {

    ......

    initLifecycle(vm)
    // 事件监听初始化
    initEvents(vm)
    initRender(vm)
    callHook(vm, 'beforeCreate')
    initInjections(vm) // resolve injections before data/props
    // 初始化 vm 状态 prop/data/computed/watch 实现初始化
    initState(vm)
    initProvide(vm) // resolve provide after data/props
    callHook(vm, 'created')

    ......

    // 配置项里有 el 属性, 则会挂载到实在 DOM 上, 实现视图的渲染
    // 这里的 $mount 办法,实质上调用了 core/instance/liftcycle.js 中的 mountComponent 办法
    if (vm.$options.el) {vm.$mount(vm.$options.el)
    }
  }
}

在这里咱们怎么了解这个挂载状态呢?先来看 Vue 官网给的一段形容

  • 如果 Vue 实例在实例化时没有收到 el 选项,则它处于 “未挂载” 状态,没有关联的 DOM 元素。
  • 能够应用 vm.$mount() 手动地挂载一个未挂载的实例。
  • 如果没有提供 elementOrSelector 参数,模板将被渲染为文档之外的的元素。
  • 并且你必须应用 原生 DOM API 把它插入文档中。
    那咱们来看一下 $mount 外部机制吧:
 * 缓存之前的 $mount 的办法以便前面返回实例,*/
const mount = Vue.prototype.$mount
/** * 手动地挂载一个未挂载的根元素,并返回实例本身(Vue 实例) */
Vue.prototype.$mount = function (el?: string | Element,  hydrating?: boolean): Component {el = el && query(el)

  /* istanbul ignore if */
  /**   * 挂载对象不能为 body 和 html 标签   */
  if (el === document.body || el === document.documentElement) {
    process.env.NODE_ENV !== 'production' && warn(`Do not mount Vue to <html> or <body> - mount to normal elements instead.`)
    return this
  }

  const options = this.$options
  // resolve template/el and convert to render function
  /**   * 判断 $options 是否有 render 办法    * 有:判断是 String 还是 Element,获取他们的 innerHTMl   * 无:在实例 Vue 时候在 vnode 里创立一个创立一个空的正文节点 见办法 createEmptyVNode   */
  if (!options.render) {
    let template = options.template
    if (template) {if (typeof template === 'string') {if (template.charAt(0) === '#') {template = idToTemplate(template)
          /* istanbul ignore if */
          if (process.env.NODE_ENV !== 'production' && !template) {
            warn(`Template element not found or is empty: ${options.template}`,
              this
            )
          }
        }
      /**       * 获取的 Element 的类型       * 具体见   https://developer.mozilla.org/zh-CN/docs/Web/API/Element/outerHTML       */
      } else if (template.nodeType) {template = template.innerHTML} else {if (process.env.NODE_ENV !== 'production') {warn('invalid template option:' + template, this)
        }
        return this
      }
    } else if (el) {template = getOuterHTML(el)
    }
    if (template) {
      /* istanbul ignore if */
      /**       * 用于监控 compile 的性能        */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile')
      }
       // 如果不存在 render 函数,则会将模板转换成 render 函数
      const {render, staticRenderFns} = compileToFunctions(template, {
        shouldDecodeNewlines,
        shouldDecodeNewlinesForHref,
        delimiters: options.delimiters,
        comments: options.comments
      }, this)
      options.render = render
      options.staticRenderFns = staticRenderFns

      /* istanbul ignore if */
       /**       * 用于监控 compile 的性能       */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile end')
        measure(`vue ${this._name} compile`, 'compile', 'compile end')
      }
    }
  }
  return mount.call(this, el, hydrating)
}

$mount 实现的是 mountComponent 函数性能

// public mount method
Vue.prototype.$mount = function (el?: string | Element,  hydrating?: boolean): Component {el = el && inBrowser ? query(el) : undefined
  return mountComponent(this, el, hydrating)
}

那么咱们再去找一下 mountComponent 函数吧:

export function mountComponent (
  vm: Component,
  el: ?Element,
  hydrating?: boolean
): Component {
  vm.$el = el
  // 如果不存在 render 函数,则间接创立一个空的 VNode 节点
  if (!vm.$options.render) {
    vm.$options.render = createEmptyVNode
    if (process.env.NODE_ENV !== 'production') {
      /* istanbul ignore if */
      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 {
        warn(
          'Failed to mount component: template or render function not defined.',
          vm
        )
      }
    }
  }
  // 检测完 render 后,开始调用 beforeMount 申明周期
  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 = () => {
       // 这里是下面所说的观察者,这里留神第二个 expOrFn 参数是一个函数
      // 会在 new Watcher 的时候通过 get 办法执行一次
      // 也就是会触发第一次 Dom 的更新
      vm._update(vm._render(), hydrating)
    }
  }

vm._watcher = new Watcher(vm, updateComponent, noop, null, true /* isRenderWatcher */)
  hydrating = false

  // 触发 $mount 函数
  if (vm.$vnode == null) {
    vm._isMounted = true
    callHook(vm, 'mounted')
  }
  return vm
}

总结来说就是:

  • 执行vm._watcher = new Watcher(vm, updateComponent, noop)
  • 触发 Watcher 外面的 get 办法,设置Dep.target = watcher
  • 执行 updateComponent
    这个过程中,会去读取咱们绑定的数据, 因为之前咱们通过 Observer 进行了数据劫持,这样会触发数据的 get 办法。此时会将 watcher 增加到 对应的 dep 中。当有数据更新时,通过 dep.notify() 去告诉到 Watcher,而后执行Watcher 中的 update 办法。此时又会去从新执行 updateComponent,至此实现对视图的从新渲染。
  • 参考 vue 实战视频解说:进入学习

咱们着重关注一下vm._update(vm._render(), hydrating):

...

    let vnode
    try {vnode = render.call(vm._renderProxy, vm.$createElement)
    } catch (e) {handleError(e, vm, `render`)
      // return error render result,
      // or previous vnode to prevent render error causing blank component
      /* istanbul ignore else */
      if (process.env.NODE_ENV !== 'production') {if (vm.$options.renderError) {
          try {vnode = vm.$options.renderError.call(vm._renderProxy, vm.$createElement, e)
          } catch (e) {handleError(e, vm, `renderError`)
            vnode = vm._vnode
          }
        } else {vnode = vm._vnode}
      } else {vnode = vm._vnode}
    }

咱们看到有俩个办法 vm._renderProxy 代理 vm,要来检测render 是否用了 vm 上没有的属性与办法,用来报错,vm.$createElement则是创立VNode:

render: function (createElement) {return createElement('h1', '题目')
}

数据咱们是晓得怎么更新的,那么组件 tamplate 到实在 dom 是怎么更新的呢?

  • 解析 tamplate 生成字符串
  • render Function解决字符串生成VNode
  • patch diff算法解决VNode
正文完
 0