简体   繁体   English

在Angular中使用JIT编译而不是使用AOT有什么优势?

[英]Is there any advantage of using JIT compilation in Angular in favor of using AOT?

The Angular docs specify several reasons for using AOT compilation in favor of JIT: Angular文档指定了使用AOT编译来支持JIT的几个原因:

  • Faster rendering 更快的渲染
  • Fewer asynchronous requests 异步请求更少
  • Smaller Angular framework download size 较小的Angular框架下载大小
  • Detect template errors earlier 尽早检测模板错误
  • Better security 更好的安全性

However, when looking for arguments to use JIT I found none. 但是,当寻找使用JIT的参数时,我什么也没找到。 Moreover, after upgrading from Angular 5.2 to Angular 8 I suddenly get a strange error when running a dev build (using JIT). 此外, 从Angular 5.2升级到Angular 8后,在运行开发版本(使用JIT)时突然出现一个奇怪的错误。 The error is: 错误是:

ERROR in ./src/app/shared/app-configuration/shared/app-configuration.model.ts 22:16-35
"export 'IMyComponents' was not found in '@mycompany/mypackage'

When running a prod build (using AOT) everything was fine. 当运行产品构建(使用AOT)时,一切都很好。 This surprised me as I never ran into an Angular compilation problem in which the prod build succeeded and the dev build failed . 这让我感到惊讶,因为我从未遇到过Angular编译问题,其中prod构建成功 ,而dev构建失败

So my assumption is that JIT is only suitable for development builds (ie speed). 因此,我的假设是JIT仅适用于开发版本(即速度)。 And adding the --aot flag can be done safely without any problem. 并且添加--aot标志可以安全地完成,没有任何问题。 Or am I missing something? 还是我错过了什么?

You're right, Angular offers 2 ways to bind your application: 没错,Angular提供了两种绑定应用程序的方法:

Just-in-Time (JIT) , which compiles your app in the browser at runtime. 即时(JIT) ,可在运行时在浏览器中编译您的应用。 (when you run ng serve ) (当您运行ng serve

  • Compiled in the browser 在浏览器中编译
  • Each files compiled separately 每个文件分别编译
  • No need to build after changing your code and before reloading the browser page 更改代码后且重新加载浏览器页面之前无需构建
  • Suitable for local development 适合当地发展

Ahead-of-Time (AOT) , which compiles your app at build time. 提前(AOT) ,可在构建时编译您的应用程序。 (when you run ng serve --aot=true ) (当您运行ng serve --aot=true

  • Compiled by the machine itself, via the command line (Faster) 由机器本身通过命令行编译(更快)
  • All code compiled together, inlining HTML/CSS in the scripts 所有代码一起编译,在脚本中内联HTML / CSS
  • No need to deploy the compiler 无需部署编译器
  • Suitable for production builds 适合生产

The ng build command with the --prod meta-flag (ng build --prod) compiles with AOT by default. 默认情况下,带有--prod元标记的ng build命令(ng build --prod)会使用AOT进行编译。

The Angular Ahead-of-Time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. 在浏览器下载并运行该代码之前,Angular Ahead-of-Time(AOT)编译器会在构建阶段将Angular HTML和TypeScript代码转换为高效的JavaScript代码。 Compiling your application during the build process provides a faster rendering in the browser. 在构建过程中编译应用程序可在浏览器中提供更快的渲染速度。

As JIT compiles your app at runtime, it can optimize the compilation and only build necessary code. 由于JIT在运行时编译您的应用程序,因此它可以优化编译并仅构建必要的代码。 So in development mode, it's common to use JIT to save the time of a full build. 因此,在开发模式下,通常使用JIT来节省完整构建的时间。 The compilation time will be faster using JIT. 使用JIT可以加快编译时间。

AOT optimizes the running speed but the compilation time is longer, thats why it's common to use it in production. AOT优化了运行速度,但是编译时间更长,这就是为什么在生产中经常使用它的原因。 AOT will also optimize the size of your application as all files will be compiled before running it. AOT还可以优化应用程序的大小,因为所有文件都将在运行之前进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM