Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简述 Vue 中模板编译的过程 #36

Open
janeLLLL opened this issue Nov 23, 2020 · 0 comments
Open

简述 Vue 中模板编译的过程 #36

janeLLLL opened this issue Nov 23, 2020 · 0 comments

Comments

@janeLLLL
Copy link
Owner

  1. 缓存公共的mount函数,重写浏览器平台的mount
  2. 先判断是否传入了render函数
    • 没有的话,是否传入了template
      • 有template,则获取el节点的outerHTML作为template
      • 没有template,则调用complie(template,options)
  3. 调用baseCompile函数:合并options,并调用baseCompile(template, trim(), finalOptions)
  4. baseCompile(template, trim(), finalOptions)中:
    • 解析器(parse)将模板字符串的模板编译转换成AST抽象语法树
    • 优化器(optimize)对AST进行静态节点标记,主要用来做虚拟DOM的渲染优化。检测到静态子树,设置为静态,不需要再每次重新渲染的时候重新生成节点;patch阶段跳过静态子树
    • 通过generte()将AST抽象语法树转换为render函数的js字符串
  5. compileToFunctions()中:
    • 将render函数通过createFunction函数转换为一个可执行的函数
    • render和staticRenderFns初始化完毕,挂载到Vue实例的options对应的属性中
  6. 执行公共的mount函数

模板编译过程

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant