【发布时间】:2020-12-31 04:31:36
【问题描述】:
我有两个可观察的管道。我需要一个接一个地运行并比较两个值是否相等。我尝试了下面的代码。这应该可以工作,当第一个 observable 值发出时,它应该去获取第二个 obserbla 值并且应该首先返回值。我需要一些专家的帮助,以更好地重构这段代码。
this.selectedUnitDetailModel$.pipe(shareReplayUntil(this.destroySub)).subscribe(
(res: UnitDetail) =>{
if(res.unitTwo){
this.appStore.select(selectUnit).
pipe(shareReplayUntil(this.destroySub)).subscribe(
(unitId: string) => {
if(unitId === res.unitTwo){
this.sameUnit = true;
}else{
this.sameUnit = false;
}
});
}
}
);
【问题讨论】:
-
试试
this.selectedUnitDetailModel$.pipe(withLatestFrom(this.appStore.select(selectUnit)), map(([{unitTwo}, unitId]) => unitTwo === unitId), tap(console.table), shareReplayUntil(this.destroySub)).subscribe(sameUnit => {this.sameUnit = sameUnit;})。 -
什么是
shareReplayUntil? -
@RafiHenig 我创建了主题并将所有管道绑定到它,当组件销毁时,我将取消订阅该主题。实际上它曾经取消订阅 observable
标签: angular typescript rxjs ngrx rxjs-pipeable-operators