6. 模板编译原理

2020-10-01  本文已影响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>
上一篇下一篇

猜你喜欢

热点阅读