我通常倾向于在组件中使用可观察对象,方法是使用myFunction(myObservable$ | async)
通过模板将它们传递到函数中,这样做效果很好。
但是,在我的模板中,我有一个输出函数,它将值发送到父组件。
<my-component
[myInput]="something$ | async"
(childOutputEmitter)="onChildEmitFunction($event)">
</my-component>
问题是我需要将发射值($event)和myObservable$ | async
值组合在一起的onChildEmitFunction()
函数。
但这似乎不起作用:
<my-component
[myInput]="something$ | async"
(childOutputEmitter)="onChildEmitFunction($event, (myObservable$ | async))">
</my-component>
有没有一种方法可以将myObservable$的值传递给onChildEmitFunction(),而不需要订阅它并将值存储在另一个变量中?
发布于 2020-05-13 11:53:09
您可以使用*ngIf
指令多次使用observable中的值。
<ng-container *ngIf="something$ | async as something">
<my-component
[myInput]="something"
(childOutputEmitter)="onChildEmitFunction($event, something)">
</my-component>
</ng-container>
还要注意的是,如果async
管道与HTTP一起使用,则每个管道都可能触发一个单独的请求。
https://stackoverflow.com/questions/61773760
复制