我习惯在Angular
HTML模板中使用带有“as”的异步管道,以避免复制可观察的订阅,如下所示:
<component *ngIf="(selected$| async) as selected"></component>
那么我可以在模板中的任何其他地方使用“选中”.
但是如果我尝试像这样使用它,在输入中:
<component [param]="(selected$| async) as selected"></component>
我收到一个错误:
Unexpected token 'as' at column 21 in [categories$| async as categories]
知道为什么吗?
谢谢!
解决方法
这是正确的,as语法特定于* ngIf.它不是可以在Angular模板中的任何位置使用的通用关键字.
请参阅https://angular.io/api/common/NgIf并搜索NgIfAs类.