Vue2.x源码解读-模板编译

模板编译作用
   模板编译会将模板转换成返回VNode(虚拟DOM)的render函数。根据编译的时间不同分为运行时编译和构建时编译。.vue单文件组件将在webpack的构建过程中转换换乘render函数。
   Vue Template Explore是一个在线将模板转换成render函数的工具。

模板编译入口

  模板编译的入口在$mount方法中,在处理用户传入的template时,会将用户传入的template转化为render函数。在源码的src/platforms/web/entry-runtime-with-compiler.js文件中定义了这个处理过程。

模板编译的过程

  1. $mount方法中会调用compileToFunctions方法将template转化为renderstaticRenderFns

  2. compileToFunctions方法中主要会经过一下几个过程:

    function compileToFunctions (
        template: string,
        options?: CompilerOptions,
        vm?: Component
      ): CompiledFunctionResult {
         
        options = extend({
         }, options)
        const warn = options.warn || baseWarn
        delete options.warn
    
        /* istanbul ignore if */
        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. ' 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值