Angular2 Ahead of Time(AoT)编译是如何工作的? [英] How does Angular2 Ahead of Time (AoT) compilation work?

查看:295
本文介绍了Angular2 Ahead of Time(AoT)编译是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 2带有名为Ahead of Time(AoT)的新功能。但经过一些阅读,我仍然无法真正理解它。它是如何工作的?它将如何带来更好的性能?与JIT有什么不同?

Angular 2 comes with the new feature called Ahead of Time (AoT). But after some reading, I still can't really understand it. How does it works? And how will it brings better performance? How is it different to JIT?

谢谢。

推荐答案

Angular在模块,指令,管道中的视图和装饰器中使用声明性绑定,需要JS在浏览器中解释它们的目的。

Angular uses declarative binding in views and decorators on modules, directives, pipes that need to be interpreted by JS in the browser to do what they are intended for.

离线模板编译器通过生成的静态代码替换声明性绑定和装饰器。

The offline template compiler replaces declarative binding and decorators by generated static code.

这使得Angular2组件的实例化和初始化更快,因为JS的工作量较少。在将应用程序提供给客户端之前,已经完成了组件的编译。

This make Angular2 component instantiation and initialization faster because JS has less work to do. The "compilation" of the component has already been done before the application was served to the client.

如果在运行时不使用其他需要它的功能, platform-b​​rowser-dynamic可以省略,根本不需要加载到浏览器中。

If you don't use other features that require it at runtime, the platform-browser-dynamic can be omitted and doesn't need to be loaded into the browser at all.

有一些讨论是否生成的代码没有超出浏览器动态平台的大小,但据我所知,Angular2团队进行了大量的实验和基准测试确保最佳性能。

There are some discussions whether the generated code doesn't outsize the browser-dynamic platform size but as far as I know the Angular2 team does a lot of experimenting and benchmarking to ensure the best performance.

来自AoT食谱


角度组件包含标准html和Angular
语法的混合(例如 ngIf ngFor )。

ngIf ngFor 这样的表达特定于Angular ,因此浏览器无法直接执行它们。

Expresions like ngIf and ngFor are specific to Angular, so there is no way for the browser to execute them directly.

在浏览器呈现应用程序之前,必须将Angular特定代码和模板转换为常规可执行文件
JavaScript。我们将此步骤称为编译。

Before the browser can render the application, Angular specific code and templates have to be converted to regular executable JavaScript. We refer to this step as compilation.

默认情况下,编译在运行时由浏览器执行,在
期间称为Just in Time编译(JIT)。它被称为只需
时间,因为编译会在应用程序加载时动态发生。

By default compilation is executed by the browser at runtime, during what is called Just in Time compilation (JIT). It's called "Just in Time" since compilation happens on the fly as the application loads.

JIT编译的缺点是运行时性能损失。
由于编译步骤,视图需要更长的渲染时间。它还
强制我们下载Angular编译器以及我们的应用程序
代码,因为我们在运行时需要编译器。

The downside to JIT compilation is a runtime performance penalty. Views take longer to render because of the compilation step. It also forces us to download the Angular compiler along with our application code since we will need the compiler at runtime.

这篇关于Angular2 Ahead of Time(AoT)编译是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆