如何在MergeMap中读取与存储状态有效结合的观察者



我想根据最新的状态处理有效的操作并执行一些逻辑。有一些例子,但我在MergeMap中正确接收数据时遇到了问题。

@ngrx/store 8.1。

@Injectable()
export class SitesEffects {
constructor(
private sitesService: SitesService,
private actions$: Actions, 
private store: Store<AppState>) {
}

getDataForSites$: Observable<Action> = createEffect(() =>
this.actions$.pipe(
ofType(LoadSites),
map(action => action.siteIds), //siteIds is string[]
withLatestFrom(siteIds => this.store.select(selectSites, new SiteIds(siteIds))), // items return from selector are Site[]
mergeMap(([a,b]) => {
// do some logic based on current state
console.log(a);
console.log(b);
return of(SitesLoaded(new Sites([])));
})
)
);
}

我希望"a"是来自第一个map函数(字符串数组(的siteIdb是Site的数组(Site[]from with LatestFrom(但是b在Visual Studio代码中有错误

类型"Observable"必须有一个"Symbol.iterator"方法返回迭代器.ts(2488(

并且在控制台(开发工具(中存在ERROR TypeError:undefined不是函数(对于行合并映射(([a,b](=>

所有这些函数应该如何对齐,以实现我期望在mergeMap操作符中得到的结果?

使用切换/合并映射,而不是使用LatestFrom

switchMap(siteIds =>   this.store.select(selectSites, new SiteIds(siteIds))).pipe(first(), map(sites => [siteIds, sites])))

您将获得实际的状态和id。使用第一个操作符,您可以用第一个发射(存储中的最后一个值(完成内部可观察

最新更新