RXJS 有条件地调用 observable
RXJS conditionally call observable
const observable$ = iif(() => this.shippingTabModified,
//Shipping Tab Modified True
of(this.updateShippingInfo())
.pipe(),
//IF Shipping Tab Not Modified
of(this.shippingInfoService.getShipmentInfoByCardIdObservable(this.data.CardId))
.pipe()
)
observable$.subscribe();
从上面的代码我想实现以下场景
if(foo === true)
{
updatemethod()
}
// if foo is true and update method is executed we need to wait until update method is finished then go to second method
// If foo is false we can call second method without waiting
secondmethod();
但是从我上面写的 rxjs 代码来看,它总是去更新方法,它没有考虑 this.shippingTabModified
的值
为什么不直接使用
const obs$ = this.shippingTabModified ?
of(this.updateShippingInfo()) :
this.shippingInfoService.getShipmentInfoByCardIdObservable(this.data.CardId);
obs$.subscribe();
编辑
条件调用:
const obs$ = of(this.updateShippingInfo()).pipe(
switchMap(res => this.shippingTabModified ?
of(res) :
this
.shippingInfoService
.getShipmentInfoByCardIdObservable(this.data.CardId)
)
您可以利用 concat
函数并执行类似这样的操作
const observables$ = condition ? [obs, obs] : [obs];
concat(...observables$).subscribe();
干杯
const observable$ = iif(() => this.shippingTabModified,
//Shipping Tab Modified True
of(this.updateShippingInfo())
.pipe(),
//IF Shipping Tab Not Modified
of(this.shippingInfoService.getShipmentInfoByCardIdObservable(this.data.CardId))
.pipe()
)
observable$.subscribe();
从上面的代码我想实现以下场景
if(foo === true)
{
updatemethod()
}
// if foo is true and update method is executed we need to wait until update method is finished then go to second method
// If foo is false we can call second method without waiting
secondmethod();
但是从我上面写的 rxjs 代码来看,它总是去更新方法,它没有考虑 this.shippingTabModified
为什么不直接使用
const obs$ = this.shippingTabModified ?
of(this.updateShippingInfo()) :
this.shippingInfoService.getShipmentInfoByCardIdObservable(this.data.CardId);
obs$.subscribe();
编辑
条件调用:
const obs$ = of(this.updateShippingInfo()).pipe(
switchMap(res => this.shippingTabModified ?
of(res) :
this
.shippingInfoService
.getShipmentInfoByCardIdObservable(this.data.CardId)
)
您可以利用 concat
函数并执行类似这样的操作
const observables$ = condition ? [obs, obs] : [obs];
concat(...observables$).subscribe();
干杯