浅理解vue2中的模板编译

  1. vue组件实例在初始化完成各种状态数据后,会触发vm.$mount()方法来进行模板编译阶段,有两种触发方式
javascript 复制代码
// 方法一:主动触发 new Vue({ el: '#app' })
if (vm.$options.el) {
 vm.$mount(vm.$options.el);
}

// 方法二:手动调用 new Vue().$mount("#app")
  1. 生成render渲染函数:执行$mount(el)方法,获取dom元素,判断vm.options中是否有render函数,没有的话需要先获取template的HTML片段,再执行compileToFunctions方法生成render函数。拿到render函数后接着触发mountComponent函数的执行
javascript 复制代码
Vue.prototype.$mount = function (el) {
  // 获取dom
  el = el && query(el);
  let options = this.$options;
  // 没有render函数,则需要解析 模板/el 并生成render函数
  if (!options.render) {
    // 获取模板字符串
    let template = options.template;
    if (template) {
      if (typeof template === "string") {
        // 模板第一个字符串为# 则判断该字符串为dom的id
        if (template.charAt(0) === "#") {
          template = document.querySelector(template).innerHTML;
        }
      } else if (template.nodeType) {
        // template是dom元素
        template = template.innerHTML;
      }
    } else if (el) {
      // outerHTML:dom元素的序列化HTML片段
      template = el.outerHTML;
    }
    if (template) {
      let _a = compileToFunctions(template, { outputSourceRange: true }, this);
      options.render = _a.render;
      options.staticRenderFns = _a.staticRenderFns;
    }
  }
  return mountComponent(this, el, false);
};

// 获取dom元素
function query(el) {
  if (typeof el === "string") {
    var selected = document.querySelector(el);
    if (!selected) {
      return document.createElement("div");
    }
    return selected;
  } else {
    return el;
  }
}
  1. compileToFunctions方法中,生成render函数的步骤:①.parseHTML方法生成ast语法树,主要是利用正则与字符串方法循环处理HTML片段;②.generate(ast,options)方法生成render函数,主要是将ast语法树转换成render渲染函数,源码看不动了没贴
javascript 复制代码
let cache = Object.create(null);
function compileToFunctions(template, options, vm) {
  if (cache[template]) {
    return cache[template];
  }
  let compiled = compile(template, options);
  let res = {};
  let fnGenErrors = [];
  res.render = createFunction(compiled.render, fnGenErrors);
  res.staticRenderFns = compiled.staticRenderFns.map(function (code) {
    return createFunction(code, fnGenErrors);
  });
  return (cache[key] = res);
}

let baseOptions = {
  expectHTML: true,
  modules: modules,
  directives: directives,
  isPreTag: isPreTag,
  isUnaryTag: isUnaryTag,
  mustUseProp: mustUseProp,
  canBeLeftOpenTag: canBeLeftOpenTag,
  isReservedTag: isReservedTag,
  getTagNamespace: getTagNamespace,
  staticKeys: genStaticKeys$1(modules)
};

function compile(template, options) {
  let finalOptions = Object.create(baseOptions);
  if (options) {
    for (let key in options) {
      if (key !== 'modules' && key !== 'directives') {
          finalOptions[key] = options[key];
      }
    }
  }
  let compiled = baseCompile(template.trim(), finalOptions);
  return compiled;
}

// ......
  1. 执行mountComponent方法,首先是生成el,接着执行beforeMount生命周期中的函数,接着生成渲染watcher实例,主动触发watcher实例的getter方法,getter方法就是updateComponent。 在vm._render方法中生成了**虚拟dom** (vnode),在生成虚拟dom的过程中触发了render函数,在读取模板中的响应式数据时进而实现了响应式数据的依赖收集。 在vm._update方法中将虚拟dom转成真实dom节点,实现了**DOM的挂载**,并赋值给vm.el,核心方法是patch()方法
javascript 复制代码
function mountComponent(vm, el, hydrating) {
  vm.$el = el;
  callHook$1(vm, 'beforeMount');
  let updateComponent = function () {
    vm._update(vm._render(), hydrating);
  };
  new Watcher(vm, updateComponent, noop, watcherOptions, true);
}

function _update(vnode) {
  if (!prevVnode) {
    // initial render
    vm.$el = patch(vm.$el, vnode, hydrating, false /* removeOnly */);
  }
  else {
    // updates
    vm.$el = patch(prevVnode, vnode);
  }
}
  1. 接着执行callHook$1(vm, 'mounted')方法,触发mounted生命周期中的函数执行
javascript 复制代码
function mountComponent(vm, el, hydrating) {
  // ...
  new Watcher(vm, updateComponent, noop, watcherOptions, true);
  callHook$1(vm, 'mounted');
}

模板编译

  1. 获取template HTML片段,根据HTML片段生成ast语法树;
  2. 根据ast语法树生成rende渲染函数;
  3. 执行render函数生成虚拟dom(vnode);
  4. 将虚拟dom转成真实dom节点并挂载在页面上;
相关推荐
千里码aicood2 小时前
Django+Vue 基于XGBoost与随机森林的沿海地区台风天气大数据预警系统实现
vue.js·随机森林·django
jay神3 小时前
【计算机毕业设计】基于Flask+Vue的电影数据可视化与智能推荐系统
前端·vue.js·python·信息可视化·flask·毕业设计·课程设计
2601_9621229717 小时前
nginx 部署前端vue项目
前端·vue.js·nginx
码视野17 小时前
基于 Spring Boot + Vue3 的【城市道路地下空洞塌陷微动传感监测与路面脱空沉降预警中台】设计与实现(含PRD/三端高保真源码/大屏)
vue.js·spring boot·后端
雪芽蓝域zzs17 小时前
第三节:目录结构重构(仿若依,优化层级,layout 与 components 同级)
前端·vue.js·重构
雪芽蓝域zzs20 小时前
第八节:Element Plus 基础、封装通用组件 + 路由守卫
前端·vue.js
码视野20 小时前
基于 Spring Boot + Vue3 的【智慧公厕微负压排风除臭与客流人感空间占用导引中台】设计与实现(含PRD/三端高保真源码/大屏)
前端·vue.js·人工智能·spring boot·后端
2601_962071571 天前
Java进阶(vue基础)
前端·javascript·vue.js
雪芽蓝域zzs1 天前
第十节:动态侧边栏菜单(根据路由 meta 自动渲染菜单)
javascript·vue.js·elementui