首页 > 解决方案 > 用于 ngIf 和传递数据的 AsyncPipe

问题描述

在多个绑定中使用 AsyncPipe 是不好的做法吗?我有一个带有 observable 的组件,我将它与 *ngIf 一起用作数据输入,就像这样

<sample-component 
   ngIf="sampleObs$ | async"
   [data] = "sampleObs$ | async"
></sample-component>

它按预期工作,我只是想知道这是否是一种好的做法,因为我是 Angular 的新手

标签: angularobservableasync-pipe

解决方案


我建议使用 async as

ngIf="sampleObs$ | async as sampleObs"

然后下一次调用您只需使用 sampleObs。


推荐阅读