作家
登录

深入Vue2.0底层思想–模板渲染

作者: 来源: 2017-07-25 17:01:42 阅读 我要评论

return _c('header',[_c('h1',[_v("I'm a template!")])]) 
  •  
  •   } 
  •  
  • })  
  • 模板衬着过程(重要的函数介绍)

    懂得完一些基本常识后,接下来我们讲解下模板的衬着过程

    $mount 函数,主如果获取 template,然落后入 compileToFunctions 函数。

    compileToFunctions 函数,重要将 template 编译成 render 函数。起首读缓存,没有缓存就调用 compile 办法拿到 render 函数 的字符串情势,再经由过程 new Function 的方法生成 render 函数。

    1. // 有缓存的话就直接在缓存琅绫擎拿 
    2.  
    3. const key = options && options.delimiters 
    4.  
    5.             ? String(options.delimiters) + template 
    6.  
    7.             : template 
    8.  
    9. if (cache[key]) { 
    10.  
    11.     return cache[key
    12.  
    13.  
    14. const res = {} 
    15.  
    16. const compiled = compile(template, options) // compile 后面会具体讲 
    17.  
    18. res.render = makeFunction(compiled.render) //经由过程 new Function 的方法生成 render 函数并缓存 
    19.  
    20. const l = compiled.staticRenderFns.length 
    21.  
    22. res.staticRenderFns = new Array(l) 
    23.  
    24. for (let i = 0; i  l; i++) { 
    25.  
    26.     res.staticRenderFns[i] = makeFunction(compiled.staticRenderFns[i]) 
    27.  
    28.  
    29. ...... 
    30.  
    31.  
    32. return (cache[key] = res) // 记录至缓存中  

    compile 函数就是将 template 编译成 render 函数的字符串情势,后面一末节我们会具体讲到。

    完成render办法的生成后,会进入 _mount 中进行DOM更新。该办法的核心逻辑如下:

    1. // 触发 beforeMount 生命周期钩子 
    2.  
    3. callHook(vm, 'beforeMount'
    4.  
    5. // 重点:新建一个 Watcher 并赋值给 vm._watcher 
    6.  
    7. vm._watcher = new Watcher(vm, function updateComponent () { 
    8.  
    9.   vm._update(vm._render(), hydrating) 
    10.  
    11. }, noop) 
    12.  
    13. hydrating = false 
    14.  
    15. // manually mounted instance, call mounted on self 

        推荐阅读

        拯救者Linux:我是如何给我的团队引入Linux的

      在项目早期就碰到公开的掉败后,一个有名大年夜学的 IT 团队决定将他们的 web 注册体系安排到 Linux上,此举几乎将办事器的最大年夜用户拜访量进步了 3 倍1998年,我在明尼苏达大年夜学为>>>详细阅读


      本文标题:深入Vue2.0底层思想–模板渲染

      地址:http://www.17bianji.com/lsqh/36398.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)