angular-cn/aio/content/guide/aot-compiler.md

53 KiB
Raw Blame History

The Ahead-of-Time (AOT) Compiler

预先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 的“预先AOT编译器”会在构建期间把 Angular 应用的 HTML 和 TypeScript 代码编译成高效的 JavaScript 代码,之后浏览器就可以下载并快速运行这些代码。

This guide explains how to build with the AOT compiler using different compiler options and how to write Angular metadata that AOT can compile.

本章描述了如何使用 AOT 编译器,以及如何书写能被 AOT 编译的 Angular 元数据。

Watch compiler author Tobias Bosch explain the Angular Compiler at AngularConnect 2016.

观看编译器作者 Tobias Bosch 在 AngularConnect 2016 大会里,对Angular 编译器的演讲。

{@a overview}

Angular compilation

Angular 中的编译

An Angular application consists largely of components and their HTML templates. Before the browser can render the application, the components and templates must be converted to executable JavaScript by an Angular compiler.

Angular 应用由大量组件及其 HTML 模板组成。 在浏览器渲染应用之前,组件和模板必须由 Angular 编译器转换成可执行的 JavaScript 代码。

Angular offers two ways to compile your application:

Angular 提供了两种方式来编译你的应用:

  1. Just-in-Time (JIT), which compiles your app in the browser at runtime

    即时JIT编译,它会在浏览器中运行时编译你的应用

  2. Ahead-of-Time (AOT), which compiles your app at build time.

    预先AOT编译,它会在构建时编译你的应用。

JIT compilation is the default when you run the build-only or the build-and-serve-locally CLI commands:

当你运行 buildserve 这两个 CLI 命令时 JIT 编译是默认选项:

ng build ng serve

{@a compile}

For AOT compilation, append the --aot flags to the build-only or the build-and-serve-locally CLI commands:

要进行 AOT 编译只要给这两个 CLI 命令添加 --aot 标志就行了:

ng build --aot ng serve --aot

The --prod meta-flag compiles with AOT by default.

--prod 标志也会默认使用 AOT 编译。

See the CLI documentation for details, especially the build topic.

要了解更多,请参见CLI 文档,特别是build 这个主题

{@a why-aot}

Why compile with AOT?

为什么需要 AOT 编译?

Faster rendering

渲染得更快

With AOT, the browser downloads a pre-compiled version of the application. The browser loads executable code so it can render the application immediately, without waiting to compile the app first.

使用 AOT浏览器下载预编译版本的应用程序。 浏览器直接加载运行代码,所以它可以立即渲染该应用,而不用等应用完成首次编译。

Fewer asynchronous requests

需要的异步请求更少

The compiler inlines external HTML templates and CSS style sheets within the application JavaScript, eliminating separate ajax requests for those source files.

编译器把外部 HTML 模板和 CSS 样式表内联到了该应用的 JavaScript 中。 消除了用来下载那些源文件的 Ajax 请求。

Smaller Angular framework download size

需要下载的 Angular 框架体积更小

There's no need to download the Angular compiler if the app is already compiled. The compiler is roughly half of Angular itself, so omitting it dramatically reduces the application payload.

如果应用已经编译过了,自然不需要再下载 Angular 编译器了。 该编译器差不多占了 Angular 自身体积的一半儿,所以,省略它可以显著减小应用的体积。

Detect template errors earlier

提早检测模板错误

The AOT compiler detects and reports template binding errors during the build step before users can see them.

AOT 编译器在构建过程中检测和报告模板绑定错误,避免用户遇到这些错误。

Better security

更安全

AOT compiles HTML templates and components into JavaScript files long before they are served to the client. With no templates to read and no risky client-side HTML or JavaScript evaluation, there are fewer opportunities for injection attacks.

AOT 编译远在 HTML 模版和组件被服务到客户端之前,将它们编译到 JavaScript 文件。 没有模版可以阅读,没有高风险客户端 HTML 或 JavaScript 可利用,所以注入攻击的机会较少。

{@a compiler-options}

Angular Compiler Options

Angular 编译器选项

You can control your app compilation by providing template compiler options in the tsconfig.json file along with the options supplied to the TypeScript compiler. The template compiler options are specified as members of "angularCompilerOptions" object as shown below:

你可以通过在 tsconfig.json 文件中随 TypeScript 编译选项一起提供模板编译选项来控制应用的编译方式。 这些模板编译选项都是作为 "angularCompilerOptions" 对象的成员指定的,代码如下:


{
  "compilerOptions": {
    "experimentalDecorators": true,
    ...
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "preserveWhitespaces": true,
    ...
  }
}

skipMetadataEmit

This option tells the compiler not to produce .metadata.json files. The option is false by default.

这个选项告诉编译器不要生成 .metadata.json 文件,它默认是 false

.metadata.json files contain information needed by the template compiler from a .ts file that is not included in the .d.ts file produced by the TypeScript compiler. This information contains, for example, the content of annotations (such as a component's template) which TypeScript emits to the .js file but not to the .d.ts file.

.metadata.json 文件中包含模板编译器所需的信息,这些信息来自于 .ts 文件中,但是没有包含在由 TypeScript 编译器生成的 .d.ts 文件中。 比如,这个信息包括 TypeScript 发出的注解内容如组件的模板TypeScript 把它生成到了 .js 文件中,但是没有生成到 .d.ts 文件中。

This option should be set to true if using TypeScript's --outFile option, as the metadata files are not valid for this style of TypeScript output. It is not recommeded to use --outFile with Angular. Use a bundler, such as webpack, instead.

如果使用了 TypeScript 的 --outFile 选项,那就要同时设置这个选项。因为在 TypeScript 的这种输出方式下metadata 文件是无效的。 Angular 中不建议使用 --outFile,请改用 webpack 之类的打包器代替。

This option can also be set to true when using factory summaries as the factory summaries include a copy of the information that is in the .metadata.json file.

当使用工厂汇总器factory summary这个选项也要设置为 true,因为工厂汇总器在自己的 .metadata.json 中也包含了这些信息的一个副本。

strictMetadataEmit

This option tells the template compiler to report an error to the .metadata.json file if "skipMetadataEmit" is false . This option is false by default. This should only be used when "skipMetadataEmit" is false and "skipTemplateCodeGen" is true.

这个选项告诉模板编译器如果 "skipMetadataEmit"false,那就把错误信息汇报到 .metadata.json 中。 只有当 "skipMetadataEmit"false"skipTemplateCodeGen"true 时才应该使用这个选项。

It is intended to validate the .metadata.json files emitted for bundling with an npm package. The validation is overly strict and can emit errors for metadata that would never produce an error when used by the template compiler. You can choose to suppress the error emitted by this option for an exported symbol by including @dynamic in the comment documenting the symbol.

它的设计意图是要验证为打包 npm 而生成的 .metadata.json 文件。 这种验证非常严格,因此在使用模板编译器时可能会对那些铁定不会出错的元数据文件报告一些错误。 你可以用 @dynamic 在注释中指定一些符号,来禁止对它们报告错误。

It is valid for .metadata.json files to contain errors. The template compiler reports these errors if the metadata is used to determine the contents of an annotation. The metadata collector cannot predict the symbols that are designed to use in an annotation, so it will preemptively include error nodes in the metadata for the exported symbols. The template compiler can then use the error nodes to report an error if these symbols are used. If the client of a library intends to use a symbol in an annotation, the template compiler will not normally report this until the client uses the symbol. This option allows detecting these errors during the build phase of the library and is used, for example, in producing Angular libraries themselves.

skipTemplateCodegen

This option tells the compiler to suppress emitting .ngfactory.js and .ngstyle.js files. When set, this turns off most of the template compiler and disables reporting template diagnostics. This option can be used to instruct the template compiler to produce .metadata.json files for distribution with an npm package while avoiding the production of .ngfactory.js and .ngstyle.js files that cannot be distributed to npm.

strictInjectionParameters

When set to true, this options tells the compiler to report an error for a parameter supplied whose injection type cannot be determined. When this value option is not provided or is false, constructor parameters of classes marked with @Injectable whose type cannot be resolved will produce a warning.

Note: It is recommended to change this option explicitly to true as this option will default to true in the future.

flatModuleOutFile

When set to true, this option tells the template compiler to generate a flat module index of the given file name and the corresponding flat module metadata. Use this option when creating flat modules that are packaged similarly to @angular/core and @angular/common. When this option is used, the package.json for the library should refer to the generated flat module index instead of the library index file. With this option only one .metadata.json file is produced that contains all the metadata necessary for symbols exported from the library index. In the generated .ngfactory.js files, the flat module index is used to import symbols that includes both the public API from the library index as well as shrowded internal symbols.

By default the .ts file supplied in the files field is assumed to be library index. If more than one .ts file is specified, libraryIndex is used to select the file to use. If more than one .ts file is supplied without a libraryIndex, an error is produced. A flat module index .d.ts and .js will be created with the given flatModuleOutFile name in the same location as the library index .d.ts file. For example, if a library uses public_api.ts file as the library index of the module, the tsconfig.json files field would be ["public_api.ts"]. The flatModuleOutFile options could then be set to, for example "index.js", which produces index.d.ts and index.metadata.json files. The library's package.json's module field would be "index.js" and the typings field would be "index.d.ts".

flatModuleId

This option specifies the preferred module id to use for importing a flat module. References generated by the template compiler will use this module name when importing symbols from the flat module. This is only meaningful when flatModuleOutFile is also supplied. Otherwise the compiler ignores this option.

generateCodeForLibraries

This option tells the template compiler to generate factory files (.ngfactory.js and .ngstyle.js) for .d.ts files with a corresponding .metadata.json file. This option defaults to true. When this option is false, factory files are generated only for .ts files.

This option should be set to false when using factory summaries.

fullTemplateTypeCheck

This option tells the compiler to enable the binding expression validation phase of the template compiler which uses TypeScript to validate binding expressions.

This option is false by default.

Note: It is recommended to set this to true as this option will default to true in the future.

annotateForClosureCompiler

This option tells the compiler to use Tsickle to annotate the emitted JavaScript with JsDoc comments needed by the Closure Compiler. This option defaults to false.

annotationsAs

Use this option to modify how the Angular specific annotations are emitted to improve tree-shaking. Non-Angular annotations and decorators are unnaffected. Default is static fields.

value description
decorators Leave the Decorators in-place. This makes compilation faster. TypeScript will emit calls to the __decorate helper. Use --emitDecoratorMetadata for runtime reflection. However, the resulting code will not properly tree-shake.
static fields Replace decorators with a static field in the class. Allows advanced tree-shakers like Closure Compiler to remove unused classes.

trace

This tells the compiler to print extra information while compiling templates.

enableLegacyTemplate

The use of <template> element was deprecated starting in Angular 4.0 in favor of using <ng-template> to avoid colliding with the DOM's element of the same name. Setting this option to true enables the use of the deprecated <template> element . This option is false by default. This option might be required by some third-party Angular libraries.

disableExpressionLowering

The Angular template compiler transforms code that is used, or could be used, in an annotation to allow it to be imported from template factory modules. See metadata rewriting for more information.

Setting this option to false disables this rewriting, requiring the rewriting to be done manually.

preserveWhitespaces

This option tells the compiler whether to remove blank text nodes from compiled templates. As of v6, this option is false by default, which results in smaller emitted template factory modules.

allowEmptyCodegenFiles

Tells the compiler to generate all the possible generated files even if they are empty. This option is false by default. This is an option used by bazel build rules and is needed to simplify how bazel rules track file dependencies. It is not recommended to use this option outside of the bazel rules.

enableIvy

Tells the compiler to generate definitions using the Render3 style code generation. This option defaults to false.

Not all features are supported with this option enabled. It is only supported for experimentation and testing of Render3 style code generation.

Note: Is it not recommended to use this option as it is not yet feature complete with the Render2 code generation.

Angular Metadata and AOT

Angular 元数据与 AOT

The Angular AOT compiler extracts and interprets metadata about the parts of the application that Angular is supposed to manage.

Angular 的 AOT 编译器会提取并解释应用中由 Angular 管理的各个部件的元数据

Angular metadata tells Angular how to construct instances of your application classes and interact with them at runtime.

Angular 的元数据会告诉 Angular 如何创建应用中类的实例以及如何在运行期间与它们交互。

You specify the metadata with decorators such as @Component() and @Input(). You also specify metadata implicitly in the constructor declarations of these decorated classes.

你通过装饰器来指定元数据,比如 @Component()@Input()。 你还可以在这些带装饰器的类的构造函数中隐式指定元数据。

In the following example, the @Component() metadata object and the class constructor tell Angular how to create and display an instance of TypicalComponent.

在下列范例中,@Component() 元数据对象和类的构造函数会告诉 Angular 如何创建和显示 TypicalComponent 的实例。


@Component({
  selector: 'app-typical',
  template: '<div>A typical component for {{data.name}}</div>'
)}
export class TypicalComponent {
  @Input() data: TypicalData;
  constructor(private someService: SomeService) { ... }
}

The Anglar compiler extracts the metadata once and generates a factory for TypicalComponent. When it needs to create a TypicalComponent instance, Angular calls the factory, which produces a new visual element, bound to a new instance of the component class with its injected dependency.

Angular 编译器只提取一次元数据,并且为 TypicalComponent 生成一个工厂。 当它需要创建 TypicalComponent 的实例时Angular 调用这个工厂,工厂会生成一个新的可视元素,并且把它(及其依赖)绑定到组件类的一个新实例上。

Metadata restrictions

元数据的限制

You write metadata in a subset of TypeScript that must conform to the following general constraints:

你只能使用 TypeScript 的一个子集书写元数据,它必须满足下列限制:

  1. Limit expression syntax to the supported subset of JavaScript.

    表达式语法只支持 JavaScript 的一个有限的子集。

  2. Only reference exported symbols after code folding.

    只能引用代码收缩后导出的符号。

  3. Only call functions supported by the compiler.

    只能调用编译器支持的那些函数

  4. Decorated and data-bound class members must be public.

    被装饰和用于数据绑定的类成员必须是公共public的。

The next sections elaborate on these points.

下一节将会详细解释这些问题。

How AOT works

AOT 工作原理

It helps to think of the AOT compiler as having two phases: a code analysis phase in which it simply records a representation of the source; and a code generation phase in which the compiler's StaticReflector handles the interpretation as well as places restrictions on what it interprets.

可以把 AOT 编译器看做两个阶段:在代码分析阶段,它只记录源代码,而在代码生成阶段,编译器的 StaticReflector 会解释这些结果,并为这些结果加上限制。

Phase 1: analysis

阶段 1分析

The TypeScript compiler does some of the analytic work of the first phase. It emits the .d.ts type definition files with type information that the AOT compiler needs to generate application code.

TypeScript 编译器会做一些初步的分析工作,它会生成类型定义文件.d.ts其中带有类型信息Angular 编译器需要借助它们来生成代码。

At the same time, the AOT collector analyzes the metadata recorded in the Angular decorators and outputs metadata information in .metadata.json files, one per .d.ts file.

同时AOT 收集器collector 会记录 Angular 装饰器中的元数据,并把它们输出到**.metadata.json**文件中,和每个 .d.ts 文件相对应。

You can think of .metadata.json as a diagram of the overall structure of a decorator's metadata, represented as an abstract syntax tree (AST).

你可以把 .metadata.json 文件看做一个包括全部装饰器的元数据的全景图,就像抽象语法树 (AST) 一样。

Angular's schema.ts describes the JSON format as a collection of TypeScript interfaces.

Angular 的 schema.ts 把这个 JSON 格式表示成了一组 TypeScript 接口。

{@a expression-syntax}

Expression syntax

The collector only understands a subset of JavaScript. Define metadata objects with the following limited syntax:

这个收集器只能理解 JavaScript 的一个子集。 请使用下列受限语法定义元数据对象:

Syntax语法 | Example范例 ----------------------------------- |----------------------------------- Literal object对象字面量 | {cherry: true, apple: true, mincemeat: false} Literal array数组字面量 | ['cherries', 'flour', 'sugar'] Spread in literal array字面量数组展开 | ['apples', 'flour', ...the_rest] Calls调用 | bake(ingredients) New创建对象 | new Oven() Property access属性访问 | pie.slice Array index数组索引 | ingredients[0] Identifier reference标识符引用 | Component A template string模板字符串 | `pie is ${multiplier} times better than cake` Literal string字符串字面量 | 'pi' Literal number数字字面量 | 3.14153265 Literal boolean逻辑字面量 | true Literal null空字面量 | null Supported prefix operator受支持的前缀操作符 | !cake Supported Binary operator受支持的二元操作符 | a + b Conditional operator条件操作符 | a ? b : c Parentheses括号 | (a + b)

If an expression uses unsupported syntax, the collector writes an error node to the .metadata.json file. The compiler later reports the error if it needs that piece of metadata to generate the application code.

如果表达式使用了不支持的语法,收集器就会往 .metadata.json 文件中写入一个错误节点。稍后,如果编译器用到元数据中的这部分内容来生成应用代码,它就会报告这个错误。

If you want ngc to report syntax errors immediately rather than produce a .metadata.json file with errors, set the strictMetadataEmit option in tsconfig.

如果你希望 ngc 立即汇报这些语法错误,而不要生成带有错误信息的 .metadata.json 文件,可以到 tsconfig 中设置 strictMetadataEmit 选项。


  "angularCompilerOptions": {
   ...
   "strictMetadataEmit" : true
 }

Angular libraries have this option to ensure that all Angular .metadata.json files are clean and it is a best practice to do the same when building your own libraries.

Angular 库通过这个选项来确保所有的 .metadata.json 文件都是干净的。当你要构建自己的代码库时,这也同样是一项最佳实践。

{@a function-expression}

{@a arrow-functions}

No arrow functions

The AOT compiler does not support function expressions and arrow functions, also called lambda functions.

Consider the following component decorator:


@Component({
  ...
  providers: [{provide: server, useFactory: () => new Server()}]
})

The AOT collector does not support the arrow function, () => new Server(), in a metadata expression. It generates an error node in place of the function.

When the compiler later interprets this node, it reports an error that invites you to turn the arrow function into an exported function.

You can fix the error by converting to this:


export function serverFactory() {
  return new Server();
}

@Component({
  ...
  providers: [{provide: server, useFactory: serverFactory}]
})

Beginning in version 5, the compiler automatically performs this rewritting while emitting the .js file.

{@a function-calls}

Limited function calls

The collector can represent a function call or object creation with new as long as the syntax is valid. The collector only cares about proper syntax.

But beware. The compiler may later refuse to generate a call to a particular function or creation of a particular object. The compiler only supports calls to a small set of functions and will use new for only a few designated classes. These functions and classes are in a table of below.

Folding

{@a exported-symbols}

The compiler can only resolve references to exported symbols. Fortunately, the collector enables limited use of non-exported symbols through folding.

The collector may be able to evaluate an expression during collection and record the result in the .metadata.json instead of the original expression.

For example, the collector can evaluate the expression 1 + 2 + 3 + 4 and replace it with the result, 10.

This process is called folding. An expression that can be reduced in this manner is foldable.

{@a var-declaration}

The collector can evaluate references to module-local const declarations and initialized var and let declarations, effectively removing them from the .metadata.json file.

Consider the following component definition:


const template = '<div>{{hero.name}}</div>';

@Component({
  selector: 'app-hero',
  template: template
})
export class HeroComponent {
  @Input() hero: Hero;
}

The compiler could not refer to the template constant because it isn't exported.

But the collector can fold the template constant into the metadata definition by inlining its contents. The effect is the same as if you had written:


@Component({
  selector: 'app-hero',
  template: '<div>{{hero.name}}</div>'
})
export class HeroComponent {
  @Input() hero: Hero;
}

There is no longer a reference to template and, therefore, nothing to trouble the compiler when it later interprets the collector's output in .metadata.json.

You can take this example a step further by including the template constant in another expression:


const template = '<div>{{hero.name}}</div>';

@Component({
  selector: 'app-hero',
  template: template + '<div>{{hero.title}}</div>'
})
export class HeroComponent {
  @Input() hero: Hero;
}

The collector reduces this expression to its equivalent folded string:

'<div>{{hero.name}}</div><div>{{hero.title}}</div>'.

Foldable syntax

The following table describes which expressions the collector can and cannot fold:

Syntax Foldable
Literal object yes
Literal array yes
Spread in literal array no
Calls no
New no
Property access yes, if target is foldable
Array index yes, if target and index are foldable
Identifier reference yes, if it is a reference to a local
A template with no substitutions yes
A template with substitutions yes, if the substitutions are foldable
Literal string yes
Literal number yes
Literal boolean yes
Literal null yes
Supported prefix operator yes, if operand is foldable
Supported binary operator yes, if both left and right are foldable
Conditional operator yes, if condition is foldable
Parentheses yes, if the expression is foldable

If an expression is not foldable, the collector writes it to .metadata.json as an AST for the compiler to resolve.

Phase 2: code generation

The collector makes no attempt to understand the metadata that it collects and outputs to .metadata.json. It represents the metadata as best it can and records errors when it detects a metadata syntax violation.

It's the compiler's job to interpret the .metadata.json in the code generation phase.

The compiler understands all syntax forms that the collector supports, but it may reject syntactically correct metadata if the semantics violate compiler rules.

The compiler can only reference exported symbols.

Decorated component class members must be public. You cannot make an @Input() property private or internal.

Data bound properties must also be public.


// BAD CODE - title is private
@Component({
  selector: 'app-root',
  template: '<h1>{{title}}</h1>'
})
export class AppComponent {
  private title = 'My App'; // Bad
}

{@a supported-functions}

Most importantly, the compiler only generates code to create instances of certain classes, support certain decorators, and call certain functions from the following lists.

New instances

The compiler only allows metadata that create instances of the class InjectionToken from @angular/core.

Annotations/Decorators

The compiler only supports metadata for these Angular decorators.

Decorator Module
Attribute @angular/core
Component @angular/core
ContentChild @angular/core
ContentChildren @angular/core
Directive @angular/core
Host @angular/core
HostBinding @angular/core
HostListener @angular/core
Inject @angular/core
Injectable @angular/core
Input @angular/core
NgModule @angular/core
Optional @angular/core
Output @angular/core
Pipe @angular/core
Self @angular/core
SkipSelf @angular/core
ViewChild @angular/core

Macro-functions and macro-static methods

The compiler also supports macros in the form of functions or static methods that return an expression.

For example, consider the following function:


export function wrapInArray<T>(value: T): T[] {
  return [value];
}

You can call the wrapInArray in a metadata definition because it returns the value of an expression that conforms to the compiler's restrictive JavaScript subset.

You might use wrapInArray() like this:


@NgModule({
  declarations: wrapInArray(TypicalComponent)
})
export class TypicalModule {}

The compiler treats this usage as if you had written:


@NgModule({
  declarations: [TypicalComponent]
})
export class TypicalModule {}

The collector is simplistic in its determination of what qualifies as a macro function; it can only contain a single return statement.

The Angular RouterModule exports two macro static methods, forRoot and forChild, to help declare root and child routes. Review the source code for these methods to see how macros can simplify configuration of complex NgModules.

{@a metadata-rewriting}

Metadata rewriting

The compiler treats object literals containing the fields useClass, useValue, useFactory, and data specially. The compiler converts the expression initializing one of these fields into an exported variable, which replaces the expression. This process of rewriting these expressions removes all the restrictions on what can be in them because the compiler doesn't need to know the expression's value—it just needs to be able to generate a reference to the value.

You might write something like:


class TypicalServer {

}

@NgModule({
  providers: [{provide: SERVER, useFactory: () => TypicalServer}]
})
export class TypicalModule {}

Without rewriting, this would be invalid because lambdas are not supported and TypicalServer is not exported.

To allow this, the compiler automatically rewrites this to something like:


class TypicalServer {

}

export const ɵ0 = () => new TypicalServer();

@NgModule({
  providers: [{provide: SERVER, useFactory: ɵ0}]
})
export class TypicalModule {}

This allows the compiler to generate a reference to ɵ0 in the factory without having to know what the value of ɵ0 contains.

The compiler does the rewriting during the emit of the .js file. This doesn't rewrite the .d.ts file, however, so TypeScript doesn't recognize it as being an export. Thus, it does not pollute the ES module's exported API.

Metadata Errors

The following are metadata errors you may encounter, with explanations and suggested corrections.

Expression form not supported
Reference to a local (non-exported) symbol
Only initialized variables and constants
Reference to a non-exported class
Reference to a non-exported function
Function calls are not supported
Destructured variable or constant not supported
Could not resolve type
Name expected
Unsupported enum member name
Tagged template expressions are not supported
Symbol reference expected


Expression form not supported

The compiler encountered an expression it didn't understand while evalutating Angular metadata.

Language features outside of the compiler's restricted expression syntax can produce this error, as seen in the following example:


// ERROR
export class Fooish { ... }
...
const prop = typeof Fooish; // typeof is not valid in metadata
  ...
  // bracket notation is not valid in metadata
  { provide: 'token', useValue: { [prop]: 'value' } };
  ...

You can use typeof and bracket notation in normal application code. You just can't use those features within expressions that define Angular metadata.

Avoid this error by sticking to the compiler's restricted expression syntax when writing Angular metadata and be wary of new or unusual TypeScript features.


{@a reference-to-a-local-symbol}

Reference to a local (non-exported) symbol

Reference to a local (non-exported) symbol 'symbol name'. Consider exporting the symbol.

The compiler encountered a referenced to a locally defined symbol that either wasn't exported or wasn't initialized.

Here's a provider example of the problem.


// ERROR
let foo: number; // neither exported nor initialized

@Component({
  selector: 'my-component',
  template: ... ,
  providers: [
    { provide: Foo, useValue: foo }
  ]
})
export class MyComponent {}

The compiler generates the component factory, which includes the useValue provider code, in a separate module. That factory module can't reach back to this source module to access the local (non-exported) foo variable.

You could fix the problem by initializing foo.


let foo = 42; // initialized

The compiler will fold the expression into the provider as if you had written this.


  providers: [
    { provide: Foo, useValue: 42 }
  ]

Alternatively, you can fix it by exporting foo with the expectation that foo will be assigned at runtime when you actually know its value.


// CORRECTED
export let foo: number; // exported

@Component({
  selector: 'my-component',
  template: ... ,
  providers: [
    { provide: Foo, useValue: foo }
  ]
})
export class MyComponent {}

Adding export often works for variables referenced in metadata such as providers and animations because the compiler can generate references to the exported variables in these expressions. It doesn't need the values of those variables.

Adding export doesn't work when the compiler needs the actual value in order to generate code. For example, it doesn't work for the template property.


// ERROR
export let someTemplate: string; // exported but not initialized

@Component({
  selector: 'my-component',
  template: someTemplate
})
export class MyComponent {}

The compiler needs the value of the template property right now to generate the component factory. The variable reference alone is insufficient. Prefixing the declaration with export merely produces a new error, "Only initialized variables and constants can be referenced".


{@a only-initialized-variables}

Only initialized variables and constants

Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.

The compiler found a reference to an exported variable or static field that wasn't initialized. It needs the value of that variable to generate code.

The following example tries to set the component's template property to the value of the exported someTemplate variable which is declared but unassigned.


// ERROR
export let someTemplate: string;

@Component({
  selector: 'my-component',
  template: someTemplate
})
export class MyComponent {}

You'd also get this error if you imported someTemplate from some other module and neglected to initialize it there.


// ERROR - not initialized there either
import { someTemplate } from './config';

@Component({
  selector: 'my-component',
  template: someTemplate
})
export class MyComponent {}

The compiler cannot wait until runtime to get the template information. It must statically derive the value of the someTemplate variable from the source code so that it can generate the component factory, which includes instructions for building the element based on the template.

To correct this error, provide the initial value of the variable in an initializer clause on the same line.


// CORRECTED
export let someTemplate = '<h1>Greetings from Angular</h1>';

@Component({
  selector: 'my-component',
  template: someTemplate
})
export class MyComponent {}


Reference to a non-exported class

Reference to a non-exported class . Consider exporting the class.

Metadata referenced a class that wasn't exported.

For example, you may have defined a class and used it as an injection token in a providers array but neglected to export that class.


// ERROR
abstract class MyStrategy { }

  ...
  providers: [
    { provide: MyStrategy, useValue: ... }
  ]
  ...

Angular generates a class factory in a separate module and that factory can only access exported classes. To correct this error, export the referenced class.


// CORRECTED
export abstract class MyStrategy { }

  ...
  providers: [
    { provide: MyStrategy, useValue: ... }
  ]
  ...


Reference to a non-exported function

Metadata referenced a function that wasn't exported.

For example, you may have set a providers useFactory property to a locally defined function that you neglected to export.


// ERROR
function myStrategy() { ... }

  ...
  providers: [
    { provide: MyStrategy, useFactory: myStrategy }
  ]
  ...

Angular generates a class factory in a separate module and that factory can only access exported functions. To correct this error, export the function.


// CORRECTED
export function myStrategy() { ... }

  ...
  providers: [
    { provide: MyStrategy, useFactory: myStrategy }
  ]
  ...


{@a function-calls-not-supported}

Function calls are not supported

Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function.

The compiler does not currently support function expressions or lambda functions. For example, you cannot set a provider's useFactory to an anonymous function or arrow function like this.


// ERROR
  ...
  providers: [
    { provide: MyStrategy, useFactory: function() { ... } },
    { provide: OtherStrategy, useFactory: () => { ... } }
  ]
  ...

You also get this error if you call a function or method in a provider's useValue.


// ERROR
import { calculateValue } from './utilities';

  ...
  providers: [
    { provide: SomeValue, useValue: calculateValue() }
  ]
  ...

To correct this error, export a function from the module and refer to the function in a useFactory provider instead.

// CORRECTED import { calculateValue } from './utilities';

export function myStrategy() { ... } export function otherStrategy() { ... } export function someValueFactory() { return calculateValue(); } ... providers: [ { provide: MyStrategy, useFactory: myStrategy }, { provide: OtherStrategy, useFactory: otherStrategy }, { provide: SomeValue, useFactory: someValueFactory } ] ...


{@a destructured-variable-not-supported}

Destructured variable or constant not supported

Referencing an exported destructured variable or constant is not supported by the template compiler. Consider simplifying this to avoid destructuring.

The compiler does not support references to variables assigned by destructuring.

For example, you cannot write something like this:

// ERROR import { configuration } from './configuration';

// destructured assignment to foo and bar const {foo, bar} = configuration; ... providers: [ {provide: Foo, useValue: foo}, {provide: Bar, useValue: bar}, ] ...

To correct this error, refer to non-destructured values.

// CORRECTED import { configuration } from './configuration'; ... providers: [ {provide: Foo, useValue: configuration.foo}, {provide: Bar, useValue: configuration.bar}, ] ...


Could not resolve type

The compiler encountered a type and can't determine which module exports that type.

This can happen if you refer to an ambient type. For example, the Window type is an ambiant type declared in the global .d.ts file.

You'll get an error if you reference it in the component constructor, which the compiler must statically analyze.


// ERROR
@Component({ })
export class MyComponent {
  constructor (private win: Window) { ... }
}

TypeScript understands ambiant types so you don't import them. The Angular compiler does not understand a type that you neglect to export or import.

In this case, the compiler doesn't understand how to inject something with the Window token.

Do not refer to ambient types in metadata expressions.

If you must inject an instance of an ambiant type, you can finesse the problem in four steps:

  1. Create an injection token for an instance of the ambiant type.

  2. Create a factory function that returns that instance.

  3. Add a useFactory provider with that factory function.

  4. Use @Inject to inject the instance.

Here's an illustrative example.

// CORRECTED import { Inject } from '@angular/core';

export const WINDOW = new InjectionToken('Window'); export function _window() { return window; }

@Component({ ... providers: [ { provide: WINDOW, useFactory: _window } ] }) export class MyComponent { constructor (@Inject(WINDOW) private win: Window) { ... } }

The Window type in the constructor is no longer a problem for the compiler because it uses the @Inject(WINDOW) to generate the injection code.

Angular does something similar with the DOCUMENT token so you can inject the browser's document object (or an abstraction of it, depending upon the platform in which the application runs).

import { Inject } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser';

@Component({ ... }) export class MyComponent { constructor (@Inject(DOCUMENT) private doc: Document) { ... } }


Name expected

The compiler expected a name in an expression it was evaluating. This can happen if you use a number as a property name as in the following example.


// ERROR
provider: [{ provide: Foo, useValue: { 0: 'test' } }]

Change the name of the property to something non-numeric.


// CORRECTED
provider: [{ provide: Foo, useValue: { '0': 'test' } }]


Unsupported enum member name

Angular couldn't determine the value of the enum member that you referenced in metadata.

The compiler can understand simple enum values but not complex values such as those derived from computed properties.

// ERROR enum Colors { Red = 1, White, Blue = "Blue".length // computed }

... providers: [ { provide: BaseColor, useValue: Colors.White } // ok { provide: DangerColor, useValue: Colors.Red } // ok { provide: StrongColor, useValue: Colors.Blue } // bad ] ...

Avoid referring to enums with complicated initializers or computed properties.


{@a tagged-template-expressions-not-supported}

Tagged template expressions are not supported

Tagged template expressions are not supported in metadata.

The compiler encountered a JavaScript ES2015 tagged template expression such as,


// ERROR
const expression = 'funky';
const raw = String.raw`A tagged template ${expression} string`;
 ...
 template: '<div>' + raw + '</div>'
 ...

String.raw() is a tag function native to JavaScript ES2015.

The AOT compiler does not support tagged template expressions; avoid them in metadata expressions.


Symbol reference expected

The compiler expected a reference to a symbol at the location specified in the error message.

This error can occur if you use an expression in the extends clause of a class.

{@a binding-expresion-validation}

Phase 3: binding expression validation

In the validation phase, the Angular template compiler uses the TypeScript compiler to validate the binding expressions in templates. Enable this phase explicity by adding the compiler option "fullTemplateTypeCheck" in the "angularCompilerOptions" of the project's tsconfig.json (see Angular Compiler Options).

Template validation produces error messages when a type error is detected in a template binding expression, similar to how type errors are reported by the TypeScript compiler against code in a .ts file.

For example, consider the following component:


@Component({
  selector: 'my-component',
  template: '{{person.addresss.street}}'
})
class MyComponent {
  person?: Person;
}

This will produce the following error:


my.component.ts.MyComponent.html(1,1): : Property 'addresss' does not exist on type 'Person'. Did you mean 'address'?

The file name reported in the error message, my.component.ts.MyComponent.html, is a synthetic file generated by the template compiler that holds contents of the MyComponent class template. Compiler never writes this file to disk. The line and column numbers are relative to the template string in the @Component annotation of the class, MyComponent in this case. If a component uses templateUrl instead of template, the errors are reported in the HTML file refereneced by the templateUrl instead of a synthetic file.

The error location is the beginning of the text node that contains the interpolation expression with the error. If the error is in an attribute binding such as [value]="person.address.street", the error location is the location of the attribute that contains the error.

The validation uses the TypeScript type checker and the options supplied to the TypeScript compiler to control how detailed the type validation is. For example, if the strictTypeChecks is specified, the error my.component.ts.MyComponent.html(1,1): : Object is possibly 'undefined' is reported as well as the above error message.

Type narrowing

The expression used in an ngIf directive is used to narrow type unions in the Angular template compiler, the same way the if expression does in TypeScript. For example, to avoid Object is possibly 'undefined' error in the template above, modify it to only emit the interpolation if the value of person is initialized as shown below:


@Component({
  selector: 'my-component',
  template: '<span *ngIf="person"> {{person.addresss.street}} </span>'
})
class MyComponent {
  person?: Person;
}

Using *ngIf allows the TypeScript compiler to infer that the person used in the binding expression will never be undefined.

Custom ngIf like directives

Directives that behave like *ngIf can declare that they want the same treatment by including a static member marker that is a signal to the template compiler to treat them like *ngIf. This static member for *ngIf is:


  public static ngIfUseIfTypeGuard: void;

This declares that the input property ngIf of the NgIf directive should be treated as a guard to the use of its template, implying that the template will only be instantiated if the ngIf input property is true.

Non-null type assertion operator

Use the non-null type assertion operator to suppress the Object is possibly 'undefined' error when it is incovienent to use *ngIf or when some constraint in the component ensures that the expression is always non-null when the binding expression is interpolated.

In the following example, the person and address properties are always set together, implying that address is always non-null if person is non-null. There is no convenient way to describe this constraint to TypeScript and the template compiler, but the error is suppressed in the example by using address!.street.


@Component({
  selector: 'my-component',
  template: '<span *ngIf="person"> {{person.name}} lives on {{address!.street}} </span>'
})
class MyComponent {
  person?: Person;
  address?: Address;

  setData(person: Person, address: Address) {
    this.person = person;
    this.address = address;
  }
}

The non-null assertion operator should be used sparingly as refactoring of the component might break this constraint.

In this example it is recommended to include the checking of address in the *ngIfas shown below:


@Component({
  selector: 'my-component',
  template: '<span *ngIf="person && address"> {{person.name}} lives on {{address.street}} </span>'
})
class MyComponent {
  person?: Person;
  address?: Address;

  setData(person: Person, address: Address) {
    this.person = person;
    this.address = address;
  }
}

Disabling type checking using $any()

Disable checking of a binding expression by surrounding the expression in a call to the $any() cast pseudo-function. The compiler treats it as a cast to the any type just like in TypeScript when a <any> or as any cast is used.

In the following example, the error Property addresss does not exist is suppressed by casting person to the any type.


@Component({
  selector: 'my-component',
  template: '{{$any(person).addresss.street}}'
})
class MyComponent {
  person?: Person;
}

Summary

小结

  • What the AOT compiler does and why it is important.

  • Why metadata must be written in a subset of JavaScript.

  • What that subset is.

  • Other restrictions on metadata definition.

  • Macro-functions and macro-static methods.

  • Compiler errors related to metadata.

  • Validation of binding expressions