Vue 2源码解读$mount函数原理

目录
  • 1. $mount 函数来源
  • 2. runtime 运行时的 $mount 函数
    • 2.1 mountComponent 函数
    • 2.2 _update 函数(首次渲染)
  • 3. runtime-with-compiler 的 $mount 函数
  • 4. runtime 对 Vue 构造函数的其他修改

1. $mount 函数来源

上一节虽然直接从 core 目录下找到了 Vue 的构造函数定义,但是缺少 $mount 方法。所以直接从开发过程中使用的 vue.esm.js 找到对应的源码入口。

根据 scripts/config.js 中可以找到 vue.esm.js 的构建入口是 entry-runtime-with-compiler-esm.ts,这个文件没有最终也是依赖的 runtime-with-compiler.ts

这里放一下依赖关系:

// src/platforms/web/entry-runtime-with-compiler-esm.ts
import Vue from './runtime-with-compiler'
export default Vue
// src/platforms/web/runtime-with-compiler.ts
import Vue from './runtime/index'
const mount = Vue.prototype.$mount
Vue.prototype.$mount = function(el?: string | Element, hydrating?: boolean): Component {
  // ...
  const options = this.$options
  // ...
  return mount.call(this, el, hydrating)
}
Vue.compile = compileToFunctions
export default Vue
// src/platforms/web/runtime/index.ts
import Vue from 'core/index'
import { mountComponent } from 'core/instance/lifecycle'
import { patch } from './patch'
// ... 处理 Vue.config
// ... 处理 Vue.options
Vue.prototype.__patch__ = inBrowser ? patch : noop
Vue.prototype.$mount = function (el?: string | Element, hydrating?: boolean ): Component {
  el = el && inBrowser ? query(el) : undefined
  return mountComponent(this, el, hydrating)
}
export default Vue

src/platforms/web/runtime/index.ts 中对 Vue 构造函数还做了其他处理,这里就先不看了。

2. runtime 运行时的 $mount 函数

src/platforms/web/runtime/index.ts 中定义的 $mount 函数就是调用了 mountComponent 方法并返回其结果,所以核心依然在 mountComponent 函数

2.1 mountComponent 函数

export function mountComponent(vm: Component, el: Element | null | undefined, hydrating?: boolean ): Component {
  vm.$el = el
  if (!vm.$options.render) {
    vm.$options.render = createEmptyVNode
    if (__DEV__) {
      // 这里是判断开发环境,如果有配置模板或者el属性,则警告需要将模板编译成渲染函数,或者使用包含编译器的部分。
    }
  }
  callHook(vm, 'beforeMount')
  let updateComponent
  if (__DEV__ && config.performance && mark) {
    // 处理开发环境中,配置了 性能分析时的处理,会在组件中创建对应的dom标签(class)
  } else {
    updateComponent = () => {
      vm._update(vm._render(), hydrating)
    }
  }
  // 渲染watcher,在执行前触发 beforeUpdate 调用对应钩子函数
  const watcherOptions: WatcherOptions = {
    before() {
      if (vm._isMounted && !vm._isDestroyed) {
        callHook(vm, 'beforeUpdate')
      }
    }
  }
  new Watcher( vm, updateComponent, noop, watcherOptions, true)
  hydrating = false
  // 这个是配合 2.7 添加的 setup 处理
  const preWatchers = vm._preWatchers
  if (preWatchers) {
    for (let i = 0; i < preWatchers.length; i++) {
      preWatchers[i].run()
    }
  }
  // 手动挂载实例,并且在首次挂载($vnode为空)时触发 mounted
  if (vm.$vnode == null) {
    vm._isMounted = true
    callHook(vm, 'mounted')
  }
  return vm
}

这里的逻辑也比较简单(省略掉了性能分析)

  • 首先判断 render 渲染函数,没有则将 render 属性配置为一个创建空节点的函数
  • 调用在 lifecycleMixin(Vue) 中定义的 _update 方法来对比和更新 VNode,并渲染到 dom 节点上
  • 实例化一个 Render Watcher ,并在每次更新 dom 之前触发 beforeUpdate
  • 在2.7+版本,根据 setup 中定义的预执行的 watcher 函数分别调用 watcher.run 执行一次
  • 最后修改实例状态 _isMounted,并触发 mounted,返回组件实例对象

2.2 _update 函数(首次渲染)

函数大致代码如下

  Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) {
    const vm: Component = this
    const prevEl = vm.$el
    const prevVnode = vm._vnode
    const restoreActiveInstance = setActiveInstance(vm)
    vm._vnode = vnode
    if (!prevVnode) {
      // 首次渲染
      vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */)
    } else {
      // 更新
      vm.$el = vm.__patch__(prevVnode, vnode)
    }
    restoreActiveInstance()
    prevEl && (prevEl.__vue__ = null)
    vm.$el && (vm.$el.__vue__ = vm)
    // if parent is an HOC, update its $el as well
    if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {
      vm.$parent.$el = vm.$el
    }
  }

这里主要是记录组件实例在更新前的 el** 和 **_vnode** 两个属性,用来在后面的 `__path__` 方法中进行比较和更新(也就是常说的 diff),最后将实例的 **el 属性的 __vue__ 指向当前实例,并处理高阶函数组件的正确dom。

这里通过 setActiveInstance(vm) 函数,用闭包的形式将 当前组件实例 导出到了外部,后面的 patch 使用的实例也就是该实例(translation 组件也会使用该实例)

3. runtime-with-compiler 的 $mount 函数

上文说到了 runtime-with-compiler.ts 中对 Vue 构造函数上的 mount∗∗函数进行了重写,并且原始的∗∗mount** 函数进行了重写,并且原始的 **mount∗∗函数进行了重写,并且原始的∗∗mount 函数也会在执行时进行相关检查。

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) {
    __DEV__ &&  warn(`Do not mount Vue to <html> or <body> - mount to normal elements instead.`)
    return this
  }
  const options = this.$options
  if (!options.render) {
    let template = options.template
    if (template) {
      // 区分template的类型并验证,最后转成字符串
    } else if (el) {
      template = getOuterHTML(el)
    }
    // 根据 template 字符串生成一个 render 函数
    if (template) {
      const { render, staticRenderFns } = compileToFunctions(
        template,
        {
          outputSourceRange: __DEV__,
          shouldDecodeNewlines,
          shouldDecodeNewlinesForHref,
          delimiters: options.delimiters,
          comments: options.comments
        },
        this
      )
      options.render = render
      options.staticRenderFns = staticRenderFns
    }
  }
  return mount.call(this, el, hydrating)
}

这里的作用其实就是编译 template 模板的过程,并且在函数执行时会检查挂载的dom节点类型,不能挂载到 body 或者 html 上。

因为本身的 mount 方法(也就是 mountComponent)只能使用 vm.render() 生成的 VNode 来进行 patch 和生成真实 dom。

4. runtime 对 Vue 构造函数的其他修改

上面说的 runtime/index.ts 中第一次定义了 Vue 上的 _mount 方法和 __patch__ 方法;同时,这里还在 Vue 构造函数上增加了其他方法。

// 定义 web 端特定的工具函数
Vue.config.mustUseProp = mustUseProp
Vue.config.isReservedTag = isReservedTag
Vue.config.isReservedAttr = isReservedAttr
Vue.config.getTagNamespace = getTagNamespace
Vue.config.isUnknownElement = isUnknownElement
// 定义相关指令和组件
extend(Vue.options.directives, platformDirectives)
extend(Vue.options.components, platformComponents)

上面的几个方法主要有以下作用:

mustUseProp: 定义哪些dom元素必须使用 props 绑定参数(默认有 input,option,select 等)

isReservedTag:判断标签是不是默认保留的标签

isReservedAttr:判断是不是标签保留属性,这里只有 class 和 style

getTagNamespace:判断标签的类别,这里只区分 SVG 元素和 math 标签

isUnknownElement:判断未知元素

然后定义了以下指令和方法:

注册 v-model 和 v-show 指令

注册 Transition 和 TransitionGroup 动画组件

以上就是Vue 2源码解读$mount函数原理的详细内容,更多关于Vue 2 $mount函数的资料请关注我们其它相关文章!

(0)

相关推荐

  • Vue $mount实战之实现消息弹窗组件

    之前的项目一直在使用Element-UI框架,element中的Notification.Message组件使用时不需要在html写标签,而是使用js调用.那时就很疑惑,为什么element ui使用this.$notify.this.$message就可以实现这样的功能? 1.实现消息弹窗组件的几个问题 如何在任何组件中使用this.$message就可以显示消息? 如何将消息的dom节点插入到body中? 同时出现多个消息弹窗时,消息弹窗的z-index如何控制? 2.效果预览 3.代码实现

  • vue中的.$mount('#app')手动挂载操作

    在Vue构造函数时,需要配置一个el属性,如果没有没有el属性时,可以使用.$mount('#app')进行挂载. 配置了el属性: new Vue({ el:"#app", router }); 如果没有配置el属性,可以使用手动挂载$mount("#app") new Vue({ router }).$mount('#app'); var vm = new Vue({ router }); vm.$mount('#app'); 补充知识:Vue手动挂载组件$mo

  • 解析vue中的$mount

    本文主要是带领大家分析$mount. $mount所做的工作从大体来讲主要分为3步: 1.如果你的option里面没有 render 函数,那么,通过 compileToFunctions 将HTML模板编译成可以生成VNode的Render函数. 2.new 一个 Watcher 实例,触发 updateComponent 方法. 3.生成vnode,经过patch,把vnode更新到dom上. 由于篇幅有限,这里先说前两步,第三步下篇说. 好,下面具体的说.首先,我们来到 $mount 函数

  • vue $mount 和 el的区别说明

    两者在使用效果上没有任何区别,都是为了将实例化后的vue挂载到指定的dom元素中. 如果在实例化vue的时候指定el,则该vue将会渲染在此el对应的dom中,反之,若没有指定el,则vue实例会处于一种"未挂载"的状态,此时可以通过$mount来手动执行挂载. 注:如果$mount没有提供参数,模板将被渲染为文档之外的的元素,并且你必须使用原生DOM API把它插入文档中. 例如: var MyComponent = Vue.extend({ template: '<div&g

  • 每天学点Vue源码之vm.$mount挂载函数

    在vue实例中,通过$mount()实现实例的挂载,下面来分析一下$mount()函数都实现了什么功能. $mount函数执行位置 _init这个私有方法是在执行initMixin时候绑定到Vue原型上的. $mount函数是如如何把组件挂在到指定元素 $mount函数定义位置 $mount函数定义位置有两个: 第一个是在src/platforms/web/runtime/index.js 这里的$mount是一个public mount method.之所以这么说是因为Vue有很多构建版本,

  • Vue 2源码解读$mount函数原理

    目录 1. $mount 函数来源 2. runtime 运行时的 $mount 函数 2.1 mountComponent 函数 2.2 _update 函数(首次渲染) 3. runtime-with-compiler 的 $mount 函数 4. runtime 对 Vue 构造函数的其他修改 1. $mount 函数来源 上一节虽然直接从 core 目录下找到了 Vue 的构造函数定义,但是缺少 $mount 方法.所以直接从开发过程中使用的 vue.esm.js 找到对应的源码入口.

  • Vue 2源码解析ParseHTML函数HTML模板

    ParseHTML函数 - HTML 模板解析 之前在解析 parse 函数时,我们知道整个 解析 template 模板并生成 ast 对象 的过程都发生在这个函数的执行过程中. 但是 parse 函数内部本身只定义了一些标签.指令的处理方法和警告函数,并且在传递给 parseHTML 函数的参数中定义了四个处理方法. 最终是通过调用 parseHTML 来解析 template 模板 整个解析过程,其实就是 通过一系列正则表达式来匹配 template 模板字符串,并截取该部分匹配内容并重新

  • Vue 2源码解析Parse函数定义

    目录 Parse 函数 parseHTML Parse 函数 在 baseCompile() 执行过程中,首先就是通过 parse方法 解析 template模板字符串,生成对应的 AST 抽象语法树. 整个 parse函数 定义太长,这里省略几个内部方法 /** * Convert HTML string to AST. */ export function parse(template: string, options: CompilerOptions): ASTElement { warn

  • vue parseHTML源码解析hars end comment钩子函数

    目录 引言 chars源码: parseText end 源码: 引言 接上文  parseHTML 函数源码解析(六) start钩子函数 接下来我们主要讲解当解析器遇到一个文本节点时会如何为文本节点创建元素描述对象,又会如何对文本节点做哪些特殊的处理. parseHTML(template, { chars: function(){ //... }, //... }) chars源码: chars: function chars(text) { if (!currentParent) { {

  • Vue 2源码解析HTMLParserOptions.start函数方法

    目录 HTMLParserOptions.start() 处理后的 input ast element HTMLParserOptions.start() 用来解析标签的开始部分(匹配到标签开始部分时调用),主要区分标签类型.解析标签指令配置与动态绑定参数等等. let root let currentParent function start(tag, attrs, unary, start, end) { const ns = (currentParent && currentPare

  • 详解Vue-Router源码分析路由实现原理

    深入Vue-Router源码分析路由实现原理 使用Vue开发SPA应用,离不开vue-router,那么vue和vue-router是如何协作运行的呢,下面从使用的角度,大白话帮大家一步步梳理下vue-router的整个实现流程. 到发文时使用的版本是: - vue (v2.5.0) - vue-router (v3.0.1) 一.vue-router 源码结构 github 地址:https://github.com/vuejs/vue-router components下是两个组件<rout

  • 详解vue mint-ui源码解析之loadmore组件

    本文介绍了vue mint-ui源码解析之loadmore组件,分享给大家,具体如下: 接入 官方接入文档mint-ui loadmore文档 接入使用Example html <div id="app"> <mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" :max-dis

  • vue router 源码概览案例分析

    源码这个东西对于实际的工作其实没有立竿见影的效果,不会像那些针对性极强的文章一样看了之后就立马可以运用到实际项目中,产生什么样的效果,源码的作用是一个潜移默化的过程,它的理念.设计模式.代码结构等看了之后可能不会立即知识变现(或者说变现很少),而是在日后的工作过程中悄无声息地发挥出来,你甚至都感觉不到这个过程 另外,优秀的源码案例,例如 vue . react 这种,内容量比较庞大,根本不是三篇五篇十篇八篇文章就能说完的,而且写起来也很难写得清楚,也挺浪费时间的,而如果只是分析其中一个点,例如

  • Vue编译器源码分析compileToFunctions作用详解

    目录 引言 Vue.prototype.$mount函数体 源码出处 options.delimiters & options.comments compileToFunctions函数逐行分析 createFunction 函数源码 引言 Vue编译器源码分析 接上篇文章我们来分析:compileToFunctions的作用. 经过前面的讲解,我们已经知道了 compileToFunctions 的真正来源你可能会问为什么要弄的这么复杂?为了搞清楚这个问题,我们还需要继续接触完整的代码. 下面

  • 详解从Vue.js源码看异步更新DOM策略及nextTick

    写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/answershuto/learnVue. 在学习过程中,为Vue加上了中文的注释https://github.com/answershuto/learnVue/tree/master/vue-src,希望可以对其他想学习Vue源码的小伙伴有所帮助. 可能会有理解存在偏差的地方,欢迎提issue指出,

随机推荐