美文网首页
6. 模板编译原理

6. 模板编译原理

作者: 一土二月鸟 | 来源:发表于2020-10-01 21:16 被阅读0次
    1. 借助vue-template-compiler的browser.js,通过一系列正则,将template转成ast抽象语法树。
    2. 将ast转为可运行的代码code(with(this){ return _c('div', [_s(aaa)]) })
    3. 通过new Function执行上面的with字符串,生成虚拟dom;
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="./node_modules/vue-template-compiler/browser.js"></script>
      <title>Document</title>
    </head>
    <body>
      <script>
        let { ast, render } = VueTemplateCompiler.compile('<div>{{aaa}}</div>');
        console.log(ast, render);
        console.log(new Function(render));
      </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:6. 模板编译原理

          本文链接:https://www.haomeiwen.com/subject/eqlvuktx.html