如何结合存储状态有效读取MergeMap中的观察者?
How to read observers in MergeMap in effect combined with store state?
我想根据最新状态处理有效的操作并执行一些逻辑。有一些示例,但我无法正确接收 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' 是来自第一个地图函数(字符串数组)的站点 ID
b 是站点数组(来自 withLatestFrom 的站点[])
但
b 在 Visual Studio 代码
中有错误
Type 'Observable' must have a 'Symbol.iterator' method that
returns an iterator.ts(2488)
并且在控制台(开发工具)中出现错误类型错误:未定义不是函数(对于行 mergeMap(([a,b]) =>
应如何调整所有这些功能以实现我期望在 mergeMap 运算符中收到的内容?
使用切换/合并映射而不是 withLatestFrom
switchMap(siteIds => this.store.select(selectSites, new SiteIds(siteIds))).pipe(first(), map(sites => [siteIds, sites])))
您将获得实际状态和 ID。使用第一个运算符,您完成了第一个发射的内部可观察对象(商店中的最后一个值)
我想根据最新状态处理有效的操作并执行一些逻辑。有一些示例,但我无法正确接收 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' 是来自第一个地图函数(字符串数组)的站点 ID b 是站点数组(来自 withLatestFrom 的站点[]) 但 b 在 Visual Studio 代码
中有错误Type 'Observable' must have a 'Symbol.iterator' method that returns an iterator.ts(2488)
并且在控制台(开发工具)中出现错误类型错误:未定义不是函数(对于行 mergeMap(([a,b]) =>
应如何调整所有这些功能以实现我期望在 mergeMap 运算符中收到的内容?
使用切换/合并映射而不是 withLatestFrom
switchMap(siteIds => this.store.select(selectSites, new SiteIds(siteIds))).pipe(first(), map(sites => [siteIds, sites])))
您将获得实际状态和 ID。使用第一个运算符,您完成了第一个发射的内部可观察对象(商店中的最后一个值)