【发布时间】:2020-03-11 12:29:36
【问题描述】:
我正在尝试使用合并运算符来组合四个不同的可观察对象,它们都发出相同的类型T。我想合并四个可观察对象,以便我有一个发出 T[] 的可观察对象,然后我可以映射结果列表。
但是我注意到,当订阅合并的 observable 时,只会发出列表中第一个 observable 的结果。为什么是这样?
我认为这可能与正在使用的地图有关,但如果合并的 observable 的结果作为单个流发出,这肯定不是问题吗?
this._signalRService.categoryGroupChangeSignal.pipe(
takeUntil(this.destroyObservables),
switchMap(changedCategoryGroupId => {
return this.getAllCategoryGroups$().pipe(
map(groups => [changedCategoryGroupId, groups])
);
}
)).subscribe(([changedCategoryGroupId, groups]: [string, CategoryGroup[]]) => {
//do stuff with merged groups list
});
getAllCategoryGroups$ = (): Observable<CategoryGroup[]> => {
return this.tenantListService.tenantList.pipe(switchMap( (tenantList: Tenant[]) => {
return merge(
this._categoryGroupService.getCustomTenantCategoryGroups(tenantList[0].id),
this._categoryGroupService.getCustomTenantCategoryGroups(tenantList[1].id),
this._categoryGroupService.getCustomTenantCategoryGroups(tenantList[2].id)
)
}));
};
【问题讨论】:
-
你有机会显示你在哪里订阅它吗?或者创建一个堆栈闪电战?
标签: javascript rxjs