问题描述
嗨,我有一个 angular2 项目。 我有两个 rxjs 表达式。我怎样才能把它们结合起来。以下是我的 rxjs 表达式。
this.trafficTypeControl.valueChanges
.pipe(
filter(x => x !== null),switchMap(x => {
if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {
this.campaignControl.setValue(null);
this.flightControl.setValue(null);
this.campaigns = [];
this.flights = [];
}
return x;
})
).subscribe();
this.trafficTypeControl.valueChanges
.pipe(
filter(x => x !== null),switchMap(x => {
if (x === TrafficType.CAMPAIGN) {
return this.campaignsService.getUnpaginatedCampaignsWithinRange({
accountIds: this.currentUser.account
? this.currentUser.account.id
: null,campaignStatuses: this.campaignStatusIds,rangeStartDate: this.rangeControl.value[0].toISOString(),rangeEndDate: this.rangeControl.value[1].toISOString()
});
}
}),tap(campaigns => {
console.log("TrafficType.CAMPAIGN :" + JSON.stringify(campaigns));
this.campaigns = campaigns;
})
).subscribe();
我想要一个包含两种逻辑的表达式。
感谢您能提供帮助 谢谢
解决方法
嗨,我改写如下
this.trafficTypeControl.valueChanges
.pipe(
filter(x => x !== null),switchMap(x => {
if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {
this.campaignControl.setValue(null);
this.flightControl.setValue(null);
this.campaigns = [];
this.flights = [];
return [];
} else if (x === TrafficType.CAMPAIGN) {
return this.campaignsService.getUnpaginatedCampaignsWithinRange({
accountIds: this.currentUser.account
? this.currentUser.account.id
: null,campaignStatuses: this.campaignStatusIds,rangeStartDate: this.rangeControl.value[0].toISOString(),rangeEndDate: this.rangeControl.value[1].toISOString()
});
}
}),tap(campaigns => {
this.campaigns = campaigns;
})
).subscribe();
不确定这是更清洁的解决方案。
,不确定,但您可能更喜欢去掉 switchMap,而是在 subscribe 方法中调用 this.campaignsService.getUnpaginatedCampaignsWithinRange
。
this.trafficTypeControl.valueChanges
.pipe(
filter(x => x !== null),tap(x => {
if (x === TrafficType.TRANSACTIONAL || x === TrafficType.BULK_SEND) {
this.campaignControl.setValue(null);
this.flightControl.setValue(null);
this.campaigns = [];
this.flights = [];
}
}),switchMap(x => {
if (x === TrafficType.CAMPAIGN) {
return this.campaignsService.getUnpaginatedCampaignsWithinRange({
accountIds: this.currentUser.account
? this.currentUser.account.id
: null,rangeEndDate: this.rangeControl.value[1].toISOString()
}).pipe(
tap(campaigns => {
console.log("TrafficType.CAMPAIGN :" + JSON.stringify(campaigns));
this.campaigns = campaigns;
})
);
}
return x;
})
).subscribe();