(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.
你可以在元数据定义中调用 `wrapInArray`,因为它所返回的表达式的值满足编译器支持的 JavaScript 受限子集。
You might use `wrapInArray()` like this:
你还可以这样使用 `wrapInArray()`:
```typescript
@NgModule({
declarations: wrapInArray(TypicalComponent)
})
export class TypicalModule {}
```
The compiler treats this usage as if you had written:
编译器会把这种用法处理成你以前的写法:
```typescript
@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.
收集器决定哪些函数是宏函数是很简单的 —— 它只能包含一个 `return` 语句。
The Angular [`RouterModule`](api/router/RouterModule) exports two macro static methods, `forRoot` and `forChild`, to help declare root and child routes.
Review the [source code](https://github.com/angular/angular/blob/master/packages/router/src/router_module.ts#L139 "RouterModule.forRoot source code")
for these methods to see how macros can simplify configuration of complex [NgModules](guide/ngmodules).
Angular 的 [`RouterModule`](api/router/RouterModule) 导出了两个静态宏函数 `forRoot` 和 `forChild`,以帮助声明根路由和子路由。
查看这些方法的[源码](https://github.com/angular/angular/blob/master/packages/router/src/router_module.ts#L139 "RouterModule.forRoot source code"),以了解宏函数是如何简化复杂的 [NgModule](guide/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.
编译器会对含有 `useClass`、`useValue`、`useFactory` 和 `data` 的对象字面量进行特殊处理。
编译器会把用这些字段之一初始化的表达式转换成一个导出为一个变量,并用它替换该表达式。
这个重写表达式的过程,会消除它们受到的所有限制,因为编译器并不需要知道该表达式的值,它只要能生成对该值的引用就行了。
You might write something like:
你可以这样写:
```typescript
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.
如果不重写,这就是无效的,因为这里不支持 Lambda 表达式,而且 `TypicalServer` 也没有被导出。
To allow this, the compiler automatically rewrites this to something like:
为了允许这种写法,编译器自动把它重写成了这样:
```typescript
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.
这就让编译器能在工厂中生成一个对 `ɵ0` 的引用,而不用知道 `ɵ0` 中包含的值到底是什么。
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.
编译器会在生成 `.js` 文件期间进行这种重写。它不会重写 `.d.ts` 文件,所以 TypeScript 也不会把这个变量当做一项导出,因此也就不会污染 ES 模块中导出的 API。
## Metadata Errors
## 元数据错误
The following are metadata errors you may encounter, with explanations and suggested corrections.
你可能遇到一些元数据错误,下面是对它们的解释和纠正建议。
[Expression form not supported 【不支持此表达式格式】](#expression-form-not-supported)
[Reference to a local (non-exported) symbol【引用了局部(未导出的)符号】](#reference-to-a-local-symbol)
[Only initialized variables and constants【只允许初始化过的变量和常量】](#only-initialized-variables)
[Reference to a non-exported class【引用了未导出的类】](#reference-to-a-non-exported-class)
[Reference to a non-exported function【引用了未导出的函数】](#reference-to-a-non-exported-function)
[Function calls are not supported【不支持函数调用】](#function-calls-not-supported)
[Destructured variable or constant not supported【不支持解构变量或常量】](#destructured-variable-not-supported)
[Could not resolve type【不能解析此类型】](#could-not-resolve-type)
[Name expected【期待是名字】](#name-expected)
[Unsupported enum member name【不支持的枚举成员名】](#unsupported-enum-member-name)
[Tagged template expressions are not supported【不支持带标签函数的模板表达式】](#tagged-template-expressions-not-supported)
[Symbol reference expected【期待是符号引用】](#symbol-reference-expected)
Expression form not supported
不支持这种表达式格式
The compiler encountered an expression it didn't understand while evalutating Angular metadata.
编译器在对 Angular 元数据求值时遇到了一个它不能理解的表达式。
Language features outside of the compiler's [restricted expression syntax](#expression-syntax)
can produce this error, as seen in the following example:
除编译器[允许的表达式语法](#expression-syntax)之外的语言特性可能导致这个错误,比如下面的例子:
```
// 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.
你可以在普通的应用代码中使用 `typeof` 和方括号标记法来指定属性名,但是这些特性不能在定义 Angular 元数据的表达式中使用。
Avoid this error by sticking to the compiler's [restricted expression syntax](#expression-syntax)
when writing Angular metadata
and be wary of new or unusual TypeScript features.
在写 Angular 的元数据时,严格遵循编译器的[受限表达式语法](#expression-syntax)可以避免这个错误,此外还要小心那些新的或罕见的 TypeScript 特性。
{@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.
下面就是存在该问题的 `provider` 范例。
```
// 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.
编译器会在单独的模块中生成这个 `userValue` 提供商的代码。*那个*工厂模块不能访问*这个*源码模块,无法访问这个(未导出的)`foo` 变量。
You could fix the problem by initializing `foo`.
你可以通过初始化 `foo` 来修正这个错误。
```
let foo = 42; // initialized
```
The compiler will [fold](#folding) the expression into the provider as if you had written this.
编译器将会把这个表达式[折叠](#folding)进 `providers` 中,就像你以前的写法一样。
```
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.
另外,你也可以通过导出 `foo` 来解决它,这样 `foo` 将会在运行期间你真正知道它的值的时候被赋值。
```
// 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.
添加 `export` 的方式通常用于需要在元数据中引用变量时,如 `providers` 和 `animations`,这样编译器就可以在这些表达式中生成对已导出变量的引用了。它不需要知道这些变量的*值*。
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.
当编译器需要知道*真正的值*已生成代码时,添加 `export` 的方式就是无效的。比如这里的 `template` 属性。
```
// 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`](#only-initialized-variables)".
编译器*现在就*需要 `template` 属性的值来生成组件工厂。
仅仅有对该变量的引用是不够的。
给这个声明加上 `export` 前缀只会生成一个新的错误 "[`Only initialized variables and constants can be referenced`【只能引用初始化过的变量和常量】](#only-initialized-variables)"。
{@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_.
下面的例子试图把组件的 ` template` 属性设置为已导出的 `someTemplate` 变量的值,而这个值虽然声明过,却没有初始化过。
```
// 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.
如果你从其它模块中导入了 `someTemplate`,但那个模块中忘了初始化它,就会看到这个错误。
```
// 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.
编译器不能等到运行时才得到该模板的信息。
它必须从源码中静态获得这个 `someTemplate` 变量的值,以便生成组件工厂,组件工厂中需要包含根据这个模板来生成元素的代码。
To correct this error, provide the initial value of the variable in an initializer clause _on the same line_.
要纠正这个错误,请在*同一行*的初始化子句中初始化这个变量的值。
```
// CORRECTED
export let someTemplate = 'Greetings from Angular
';
@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.
比如,你可能定义了一个类并在某个 `providers` 数组中把它用作了依赖注入令牌,但是忘了导出这个类。
```
// 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](#exported-symbols).
To correct this error, export the referenced class.
Angular 会在一个单独的模块中生成类工厂,而那个工厂[只能访问已导出的类](#exported-symbols)。
要纠正这个问题,就要导出所引用的类。
```
// 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.
比如,你可能已经把某个服务提供商的 `useFactory` 属性设置成了一个局部定义但忘了导出的函数。
```
// 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](#exported-symbols).
To correct this error, export the function.
Angular 会在一个单独的模块中生成类工厂,那个工厂[只能访问已导出的函数](#exported-symbols)。
要纠正这个错误,请导出该函数。
```
// 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](#function-expression).
For example, you cannot set a provider's `useFactory` to an anonymous function or arrow function like this.
编译器目前不支持[函数表达式或 Lambda 表达式](#function-expression)。
比如,你不能把某个服务提供商的 `useFactory` 设置成如下匿名函数或函数表达式。
```
// 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`.
如果你在某个提供商的 `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.
要改正这个问题,就要从模块中导出这个函数,并改成在服务提供商的 `useFactory` 中引用该函数。
// 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](https://www.typescriptlang.org/docs/handbook/variable-declarations.html#destructuring).
编译器不支持引用通过[解构](https://www.typescriptlang.org/docs/handbook/variable-declarations.html#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.
这通常会发生在你引用环境类型时。
比如,`Window` 类型就是在全局 `.d.ts` 文件中声明的环境类型。
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.
TypeScript 能理解这些环境类型,所以你不用导入它们。
但 Angular 编译器不理解你没有导入或导出过的类型。
In this case, the compiler doesn't understand how to inject something with the `Window` token.
这种情况下,编译器就无法理解如何使用这个 `Window` 令牌来进行注入。
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.
为环境类型的实例创建一个注入令牌。
1. Create a factory function that returns that instance.
创建一个返回该实例的工厂函数。
1. Add a `useFactory` provider with that factory function.
使用该工厂函数添加一个 `useFactory` 提供商。
1. Use `@Inject` to inject the instance.
使用 `@Inject` 来注入这个实例。
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.
对于编译器来说,构造函数中出现 `Window` 类型已不再是个问题,因为它现在使用 `@Inject(WINDOW)` 来生成注入代码。
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).
Angular 也用 `DOCUMENT` 令牌做了类似的事情,所以你也可以注入浏览器的 `document` 对象(或它的一个抽象层,取决于该应用运行在哪个平台)。
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](https://www.typescriptlang.org/docs/handbook/enums.html)
that you referenced in metadata.
Angular 不能确定你在元数据中引用的[枚举成员](https://www.typescriptlang.org/docs/handbook/enums.html)的值。
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) such as,
当编译器遇到这样的[带标签函数的模板表达式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) 时:
```
// ERROR
const expression = 'funky';
const raw = String.raw`A tagged template ${expression} string`;
...
template: '' + raw + '
'
...
```
[`String.raw()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
is a _tag function_ native to JavaScript ES2015.
[`String.raw()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) 是一个 ES2015 原生的*标签函数*。
The AOT compiler does not support tagged template expressions; avoid them in metadata expressions.
AOT 编译器不支持带标签函数的模板表达式,避免在元数据表达式中使用它们。
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.
当你在类的 `extends` 子句中使用表达式时就会出现这个错误。
{@a binding-expresion-validation}
## Phase 3: binding expression validation
## 阶段 3:验证绑定表达式
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](#compiler-options)).
在验证阶段,Angular 的模板编译器会使用 TypeScript 编译器来验证模板中的绑定表达式。
通过在项目的 `tsconfig.json`(参见 [Angular Compiler Options](#compiler-options))的 `"angularCompilerOptions"` 中添加编译选项 `"fullTemplateTypeCheck"` 可以启用这个阶段。
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.
当模板绑定表达式中检测到类型错误时,进行模板验证时就会生成错误。这和 TypeScript 编译器在处理 `.ts` 文件中的代码时报告错误很相似。
For example, consider the following component:
比如,考虑下列组件:
```typescript
@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.
错误信息中汇报的文件名 `my.component.ts.MyComponent.html` 是一个由模板编译器生成出的虚拟文件,
用于保存 `MyComponent` 类的模板内容。
编译器永远不会把这个文件写入磁盘。这个例子中,这里的行号和列号都是相对于 `MyComponent` 的 `@Component` 注解中的模板字符串的。
如果组件使用 `templateUrl` 来代替 `template`,这些错误就会在 `templateUrl` 引用的 HTML 文件中汇报,而不是这个虚拟文件中。
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.
错误的位置是从包含出错的插值表达式的那个文本节点开始的。
如果错误是一个属性绑定,比如 `[value]="person.address.street"` ,错误的位置就是那个包含错误的属性的位置。
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.
这个验证过程使用 TypeScript 的类型检查器,这些选项也会提供给 TypeScript 编译器以控制类型验证的详细程度。
比如,如果指定了 `strictTypeChecks`,就会像上面的错误信息一样报告 ```my.component.ts.MyComponent.html(1,1): : Object is possibly 'undefined'``` 错误。
### 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:
在 `ngIf` 指令中使用的表达式用来在 Angular 模板编译器中窄化联合类型,就像 TypeScript 中的 `if` 表达式一样。
比如,要在上述模板中消除 `Object is possibly 'undefined'` 错误,可以把它改成只在 `person` 的值初始化过的时候才生成这个插值表达式。
```typescript
@Component({
selector: 'my-component',
template: ' {{person.addresss.street}} '
})
class MyComponent {
person?: Person;
}
```
Using `*ngIf` allows the TypeScript compiler to infer that the `person` used in the
binding expression will never be `undefined`.
使用 `*ngIf` 能让 TypeScript 编译器推断出这个绑定表达式中使用的 `person` 永远不会是 `undefined`。
#### Custom `ngIf` like directives
#### 类似于的 `ngIf` 的自定义指令
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:
那些行为与 `*ngIf` 类似的指令可以通过包含一个静态成员作为标记,来告诉模板编译器它们希望和 `*ngIf` 享受同等待遇。这个 `*ngIf` 的静态成员就是:
```typescript
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.
它声明了 `NgIf` 指令的 `ngIf` 属性应该在用到它的模板中看做一个守卫,以表明只有当 `ngIf` 这个输入属性为 `true` 时,才应该生成那个模板。
### Non-null type assertion operator
### 非空类型断言操作符
Use the [non-null type assertion operator](guide/template-syntax#non-null-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.
使用 [非空类型断言操作符](guide/template-syntax#non-null-assertion-operator)可以在不方便使用 `*ngIf` 或
当组件中的某些约束可以确保这个绑定表达式在求值时永远不会为空时,防止出现 `Object is possibly 'undefined'` 错误。
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`.
在下列例子中,`person` 和 `address` 属性总是一起出现的,如果 `person` 非空,则 `address` 也一定非空。没有一种简便的写法可以向 TypeScript 和模板编译器描述这种约束。但是这个例子中使用 `address!.street` 避免了报错。
```typescript
@Component({
selector: 'my-component',
template: ' {{person.name}} lives on {{address!.street}} '
})
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 `*ngIf`as shown below:
这个例子中,更建议在 `*ngIf` 中包含对 `address` 的检查,代码如下:
```typescript
@Component({
selector: 'my-component',
template: ' {{person.name}} lives on {{address.street}} '
})
class MyComponent {
person?: Person;
address?: Address;
setData(person: Person, address: Address) {
this.person = person;
this.address = address;
}
}
```
### Disabling type checking using `$any()`
### 使用 `$any()` 禁用类型检查
Disable checking of a binding expression by surrounding the expression
in a call to the [`$any()` cast pseudo-function](guide/template-syntax).
The compiler treats it as a cast to the `any` type just like in TypeScript when a ``
or `as any` cast is used.
可以通过把绑定表达式包含在[类型转换伪函数 `$any()` ](guide/template-syntax) 中来禁用类型检查。
编译器会像在 TypeScript 中使用 `` 或 `as any` 进行类型转换一样对待它。
In the following example, the error `Property addresss does not exist` is suppressed
by casting `person` to the `any` type.
下面的例子中,通过把 `person` 转换成 `any` 类型,忽略了 `Property addresss does not exist` 错误。
```typescript
@Component({
selector: 'my-component',
template: '{{$any(person).addresss.street}}'
})
class MyComponent {
person?: Person;
}
```
## Summary
## 小结
* What the AOT compiler does and why it is important.
什么是 AOT 编译器,以及它为什么如此重要。
* Why metadata must be written in a subset of JavaScript.
为何元数据必须使用 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
验证绑定表达式。