iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >如何解决Vue.js中template编译的问题
  • 733
分享到

如何解决Vue.js中template编译的问题

2024-04-02 19:04:59 733人浏览 安东尼
摘要

这篇文章主要介绍了如何解决vue.js中template编译的问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。$mount首先看一下mou

这篇文章主要介绍了如何解决vue.js中template编译的问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

$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) {
  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
 
 if (!options.render) {
  let template = options.template
  
  if (template) {
   
   if (typeof template === 'string') {
    if (template.charAt(0) === '#') {
     template = idToTemplate(template)
     
     if (process.env.NODE_ENV !== 'production' && !template) {
      warn(
       `Template element not found or is empty: ${options.template}`,
       this
      )
     }
    }
   } 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) {
   
   if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
    mark('compile')
   }

   
   const { render, staticRenderFns } = compileToFunctions(template, {
    shouldDecodeNewlines,
    delimiters: options.delimiters
   }, this)
   options.render = render
   options.staticRenderFns = staticRenderFns

   
   if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
    mark('compile end')
    measure(`${this._name} compile`, 'compile', 'compile end')
   }
  }
 }
 
 
 return mount.call(this, el, hydrating)
}

通过mount代码我们可以看到,在mount的过程中,如果render函数不存在(render函数存在会优先使用render)会将template进行compileToFunctions得到render以及staticRenderFns。譬如说手写组件时加入了template的情况都会在运行时进行编译。而render function在运行后会返回VNode节点,供页面的渲染以及在update的时候patch。接下来我们来看一下template是如何编译的。

一些基础

首先,template会被编译成AST语法树,那么AST是什么?

在计算机科学中,抽象语法树(abstract syntax tree或者缩写为AST),或者语法树(syntax tree),是源代码的抽象语法结构的树状表现形式,这里特指编程语言的源代码。

AST会经过generate得到render函数,render的返回值是VNode,VNode是Vue的虚拟DOM节点,具体定义如下:

export default class VNode {
 tag: string | void;
 data: VNodeData | void;
 children: ?Array<VNode>;
 text: string | void;
 elm: Node | void;
 ns: string | void;
 context: Component | void; // rendered in this component's scope
 functionalContext: Component | void; // only for functional component root nodes
 key: string | number | void;
 componentOptions: VNodeComponentOptions | void;
 componentInstance: Component | void; // component instance
 parent: VNode | void; // component placeholder node
 raw: boolean; // contains raw HTML? (server only)
 isStatic: boolean; // hoisted static node
 isRootInsert: boolean; // necessary for enter transition check
 isComment: boolean; // empty comment placeholder?
 isCloned: boolean; // is a cloned node?
 isOnce: boolean; // is a v-once node?
 
 
 constructor (
  tag?: string,
  data?: VNodeData,
  children?: ?Array<VNode>,
  text?: string,
  elm?: Node,
  context?: Component,
  componentOptions?: VNodeComponentOptions
 ) {
  
  this.tag = tag
  
  this.data = data
  
  this.children = children
  
  this.text = text
  
  this.elm = elm
  
  this.ns = undefined
  
  this.context = context
  
  this.functionalContext = undefined
  
  this.key = data && data.key
  
  this.componentOptions = componentOptions
  
  this.componentInstance = undefined
  
  this.parent = undefined
  
  this.raw = false
  
  this.isStatic = false
  
  this.isRootInsert = true
  
  this.isComment = false
  
  this.isCloned = false
  
  this.isOnce = false
 }

 // DEPRECATED: alias for componentInstance for backwards compat.
 
 get child (): Component | void {
  return this.componentInstance
 }
}

关于VNode的一些细节,请参考VNode节点。

createCompiler

createCompiler用以创建编译器,返回值是compile以及compileToFunctions。compile是一个编译器,它会将传入的template转换成对应的AST树、render函数以及staticRenderFns函数。而compileToFunctions则是带缓存的编译器,同时staticRenderFns以及render函数会被转换成Funtion对象。

因为不同平台有一些不同的options,所以createCompiler会根据平台区分传入一个baseOptions,会与compile本身传入的options合并得到最终的finalOptions。

compileToFunctions

首先还是贴一下compileToFunctions的代码。

 
 function compileToFunctions (
  template: string,
  options?: CompilerOptions,
  vm?: Component
 ): CompiledFunctionResult {
  options = options || {}

  
  if (process.env.NODE_ENV !== 'production') {
   // detect possible CSP restriction
   try {
    new Function('return 1')
   } catch (e) {
    if (e.toString().match(/unsafe-eval|CSP/)) {
     warn(
      'It seems you are using the standalone build of Vue.js in an ' +
      'environment with Content Security Policy that prohibits unsafe-eval. ' +
      'The template compiler cannot work in this environment. Consider ' +
      'relaxing the policy to allow unsafe-eval or pre-compiling your ' +
      'templates into render functions.'
     )
    }
   }
  }
  
  // check cache
  
  const key = options.delimiters
   ? String(options.delimiters) + template
   : template
  if (functionCompileCache[key]) {
   return functionCompileCache[key]
  }

  // compile
  
  const compiled = compile(template, options)

  // check compilation errors/tips
  if (process.env.NODE_ENV !== 'production') {
   if (compiled.errors && compiled.errors.length) {
    warn(
     `Error compiling template:\n\n${template}\n\n` +
     compiled.errors.map(e => `- ${e}`).join('\n') + '\n',
     vm
    )
   }
   if (compiled.tips && compiled.tips.length) {
    compiled.tips.forEach(msg => tip(msg, vm))
   }
  }

  // turn code into functions
  const res = {}
  const fnGenErrors = []
  
  res.render = makeFunction(compiled.render, fnGenErrors)
  
  const l = compiled.staticRenderFns.length
  res.staticRenderFns = new Array(l)
  for (let i = 0; i < l; i++) {
   res.staticRenderFns[i] = makeFunction(compiled.staticRenderFns[i], fnGenErrors)
  }

  // check function generation errors.
  // this should only happen if there is a bug in the compiler itself.
  // mostly for codegen development use
  
  if (process.env.NODE_ENV !== 'production') {
   if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {
    warn(
     `Failed to generate render function:\n\n` +
     fnGenErrors.map(({ err, code }) => `${err.toString()} in\n\n$[code]\n`).join('\n'),
     vm
    )
   }
  }

  
  return (functionCompileCache[key] = res) 
 }

我们可以发现,在闭包中,会有一个functionCompileCache对象作为缓存器。

 
 const functionCompileCache: {
  [key: string]: CompiledFunctionResult;
 } = Object.create(null)

在进入compileToFunctions以后,会先检查缓存中是否有已经编译好的结果,如果有结果则直接从缓存中读取。这样做防止每次同样的模板都要进行重复的编译工作。

  // check cache
  
  const key = options.delimiters
   ? String(options.delimiters) + template
   : template
  if (functionCompileCache[key]) {
   return functionCompileCache[key]
  }

在compileToFunctions的末尾会将编译结果进行缓存

 
 return (functionCompileCache[key] = res)

 compile

 
 function compile (
  template: string,
  options?: CompilerOptions
 ): CompiledResult {
  const finalOptions = Object.create(baseOptions)
  const errors = []
  const tips = []
  finalOptions.warn = (msg, tip) => {
   (tip ? tips : errors).push(msg)
  }

  
  if (options) {
   // merge custom modules
   
   if (options.modules) {
    finalOptions.modules = (baseOptions.modules || []).concat(options.modules)
   }
   // merge custom directives
   if (options.directives) {
    
    finalOptions.directives = extend(
     Object.create(baseOptions.directives),
     options.directives
    )
   }
   // copy other options
   for (const key in options) {
    
    if (key !== 'modules' && key !== 'directives') {
     finalOptions[key] = options[key]
    }
   }
  }

  
  const compiled = baseCompile(template, finalOptions)
  if (process.env.NODE_ENV !== 'production') {
   errors.push.apply(errors, detectErrors(compiled.ast))
  }
  compiled.errors = errors
  compiled.tips = tips
  return compiled
 }

compile主要做了两件事,一件是合并option(前面说的将平台自有的option与传入的option进行合并),另一件是baseCompile,进行模板template的编译。

来看一下baseCompile

baseCompile

function baseCompile (
 template: string,
 options: CompilerOptions
): CompiledResult {
 
 const ast = parse(template.trim(), options)
 
 optimize(ast, options)
 
 const code = generate(ast, options)
 return {
  ast,
  render: code.render,
  staticRenderFns: code.staticRenderFns
 }
}

baseCompile首先会将模板template进行parse得到一个AST语法树,再通过optimize做一些优化,最后通过generate得到render以及staticRenderFns。

parse

parse的源码可以参见https://GitHub.com/answershuto/learnVue/blob/master/vue-src/compiler/parser/index.js#L53。

parse会用正则等方式解析template模板中的指令、class、style等数据,形成AST语法树。

optimize

optimize的主要作用是标记static静态节点,这是Vue在编译过程中的一处优化,后面当update更新界面时,会有一个patch的过程,diff算法会直接跳过静态节点,从而减少了比较的过程,优化了patch的性能。

generate

generate是将AST语法树转化成render funtion字符串的过程,得到结果是render的字符串以及staticRenderFns字符串。

至此,我们的template模板已经被转化成了我们所需的AST语法树、render function字符串以及staticRenderFns字符串。

举个例子

来看一下这段代码的编译结果

<div class="main" :class="bindClass">
  <div>{{text}}</div>
  <div>hello world</div>
  <div v-for="(item, index) in arr">
    <p>{{item.name}}</p>
    <p>{{item.value}}</p>
    <p>{{index}}</p>
    <p>---</p>
  </div>
  <div v-if="text">
    {{text}}
  </div>
  <div v-else></div>
</div>

转化后得到AST树,如下图:

如何解决Vue.js中template编译的问题

我们可以看到最外层的div是这颗AST树的根节点,节点上有许多数据代表这个节点的形态,比如static表示是否是静态节点,staticClass表示静态class属性(非bind:class)。children代表该节点的子节点,可以看到children是一个长度为4的数组,里面包含的是该节点下的四个div子节点。children里面的节点与父节点的结构类似,层层往下形成一棵AST语法树。

再来看看由AST得到的render函数

with(this){
  return _c( 'div',
        {
          
          staticClass:"main",
          
          class:bindClass
        },
        [
          _c( 'div', [_v(_s(text))]),
          _c('div',[_v("hello world")]),
          
          _l(
            (arr),
            function(item,index){
              return _c( 'div',
                    [_c('p',[_v(_s(item.name))]),
                    _c('p',[_v(_s(item.value))]),
                    _c('p',[_v(_s(index))]),
                    _c('p',[_v("---")])]
                  )
            }
          ),
          
          (text)?_c('div',[_v(_s(text))]):_c('div',[_v("no text")])],
          2
      )
}

_c,_v,_s,_q

看了render function字符串,发现有大量的_c,_v,_s,_q,这些函数究竟是什么?

带着问题,我们来看一下core/instance/render。


 Vue.prototype._o = markOnce
 
 Vue.prototype._n = toNumber
 
 Vue.prototype._s = toString
 
 Vue.prototype._l = renderList
 
 Vue.prototype._t = renderSlot
 
 Vue.prototype._q = looseEqual
 
 Vue.prototype._i = looseIndexOf
 
 Vue.prototype._m = renderStatic
 
 Vue.prototype._f = resolveFilter
 
 Vue.prototype._k = checkKeyCodes
 
 Vue.prototype._b = bindObjectProps
 
 Vue.prototype._v = createTextVNode
 
 Vue.prototype._e = createEmptyVNode
 
 Vue.prototype._u = resolveScopedSlots

 
 vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)

通过这些函数,render函数最后会返回一个VNode节点,在_update的时候,经过patch与之前的VNode节点进行比较,得出差异后将这些差异渲染到真实的DOM上。

感谢你能够认真阅读完这篇文章,希望小编分享的“如何解决Vue.js中template编译的问题”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网node.js频道,更多相关知识等着你来学习!

--结束END--

本文标题: 如何解决Vue.js中template编译的问题

本文链接: https://www.lsjlt.com/news/74438.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何解决Vue.js中template编译的问题
    这篇文章主要介绍了如何解决Vue.js中template编译的问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。$mount首先看一下mou...
    99+
    2024-04-02
  • vue template中style背景设置不编译问题怎么解决
    这篇文章主要介绍了vue template中style背景设置不编译问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue template中style背景设置不编译问题怎么解决文...
    99+
    2023-06-30
  • ubuntu中如何解决php无法编译问题
    小编给大家分享一下ubuntu中如何解决php无法编译问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!ubuntu的php无法编译的解决办法:1、执行“apt-...
    99+
    2023-06-22
  • vue.js not detected的问题如何解决
    本文小编为大家详细介绍“vue.js not detected的问题如何解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“vue.js not detected的问题如何解决”文章能帮助大家解决疑惑...
    99+
    2023-07-05
  • 如何解决Linux内核编译失败的问题
    本篇内容主要讲解“如何解决Linux内核编译失败的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决Linux内核编译失败的问题”吧!内核常识我们先要了解一点.一个内核有哪些东西.比如我...
    99+
    2023-06-12
  • Android中Axml编译问题怎么解决
    在Android中,Axml编译问题可能有多种原因导致,可以尝试以下解决方法:1. 检查Axml文件的语法错误:在编辑Axml文件时...
    99+
    2023-08-09
    Android
  • 如何解决Vue编译时写在style中的路径问题
    这篇文章将为大家详细讲解有关如何解决Vue编译时写在style中的路径问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。写在vue文件里面的style样式,在添加例如背景...
    99+
    2024-04-02
  • 如何解决nginx中rtmp模块编译arm版本的问题
    这篇文章主要为大家展示了“如何解决nginx中rtmp模块编译arm版本的问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何解决nginx中rtmp模块编译arm版本的问题”这篇文章吧。一、...
    99+
    2023-06-22
  • 如何解决C#版Nebula客户端编译的问题
    小编给大家分享一下如何解决C#版Nebula客户端编译的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、需求背景从Nebula的Github上可以发现,Ne...
    99+
    2023-06-20
  • 如何解决css3中calc在less编译时被计算的问题
    这篇文章将为大家详细讲解有关如何解决css3中calc在less编译时被计算的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。对于前端er来说,Less或Sass已经是...
    99+
    2024-04-02
  • 如何理解网络Fedora gcc编译中的问题
    如何理解网络Fedora gcc编译中的问题,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。全面介绍Fedora gcc编译, 这里我发表一下个人理解,这里将介绍Fedora...
    99+
    2023-06-16
  • 如何解决feign调用中文参数被encode编译的问题
    这篇文章主要介绍如何解决feign调用中文参数被encode编译的问题,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Feign调用中文参数被encode编译原因在实现一个feign调用时使用了Post请求,并且拼接u...
    99+
    2023-06-29
  • 如何解决php编译后没有扩展库的问题
    本篇内容主要讲解“如何解决php编译后没有扩展库的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决php编译后没有扩展库的问题”吧!php编译后没有扩展库的解决方法是,首先进入php的...
    99+
    2023-06-20
  • vue中template报错问题怎么解决
    这篇文章主要讲解了“vue中template报错问题怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue中template报错问题怎么解决”吧!template报错写这个纯粹是为了纪...
    99+
    2023-06-30
  • vue中关于template报错等问题的解决
    目录template报错vue报错问题template报错 写这个纯粹是为了纪念有多蠢 template:` <div class='app'>     <butt...
    99+
    2024-04-02
  • 解决Maven多模块编译慢的问题
    目录Maven多模块编译慢完整命令如下Maven多模块编译中遇到的坑一、概述二、坑描述和解决方法踩坑描述解决方法Maven多模块编译慢 最近在部署项目时发现,Maven编译打包相当耗...
    99+
    2024-04-02
  • C++中编译优化问题和解决方法的详解
    C++中编译优化问题和解决方法的详解摘要:C++编译器优化是提高程序性能的重要手段。然而,在实际开发中,我们经常会遇到一些与编译优化相关的问题,比如编译器错误优化、代码性能下降等。本文将针对这些问题进行详细的解析,并提供相应的解决方法,以期...
    99+
    2023-10-22
    问题解决方法 C++编译优化 详解编程关键词
  • C++中常见的编译优化问题解决方案
    C++中常见的编译优化问题解决方案摘要:在使用C++编写程序时,我们经常会遇到一些性能瓶颈,影响程序的运行效率。为了提高代码的执行效率,我们可以使用编译器进行优化。本文将介绍一些常见的编译优化问题及其解决方案,并给出具体的代码示例。一、循环...
    99+
    2023-10-22
    内联优化 (Inlining Optimization) 循环展开优化 (Loop Unrolling Optimiza
  • C++中编译优化问题的详解
    C++中编译优化问题的详解编写高效的C++代码是每个程序员都追求的目标,而编译优化就是其中一个重要的方面。正确理解和应用编译优化可以极大地提高程序的性能和效率。本文将从C++编译优化的基本原理、常见的优化技术和具体的代码示例入手,详细解析C...
    99+
    2023-10-22
    C++编译优化问题
  • eclipse无法解析的编译问题怎么解决
    要解决Eclipse无法解析的编译问题,您可以尝试以下几个步骤:1. 清理项目:在Eclipse中,选择“Project”菜单,然后...
    99+
    2023-09-14
    eclipse
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作