下面是一个简短的例子:
<ng-container *ngIf="(state$ | async).foo as foo">
实际上foo是 Foo: {id:string, name:string, value: number}
Foo: {id:string, name:string, value: number}
foo 在模板IDE中,我们不知道foo有id、名称或值。
foo
福 到 Foo
福
Foo
as foo 语句是创建一个不用于强制转换的模板变量,如果这样使用的话
as foo
<ng-container *ngIf="(state$ | async).foo.id">
您将获得intellisense类型,但是当您创建模板变量时,这些信息似乎丢失了。
这可能是一个bug,将来可能会得到解决。
<ng-container *ngIf="($state | async) as foo"> {{foo | json}} <div> {{foo.id}} <!-- foo has no type information--> </div> {{value.name}} <!-- declared property has type information--> </ng-container>
stackblitz demo