![](/img/trans.png)
[英]Initialize BehaviourSubject with value from an Observable in an Angular template
[英]Set the value of a BehaviourSubject from an Observable
只是想看看是否有更好的方法可以做到這一點:
this.observable$.pipe(take(1)).subscribe((observableValue) => {
this.behaviourSubject$.next(observableValue);
});
當我說得更好時 ,我在考慮不必訂閱原始的observable並使用take(1)
我在看startWith
:
this.behaviourSubject$.pipe(
startWith(this.observable$)
)
但是startWith
不接受可觀察的。
諸如此類,將您的需求組合成一個流
this.observable$.pipe(
first(),
tap(()=>this.behaviourSubject$.next(observableValue))
switchMapTo(this.behaviourSubject$))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.