在使用 withLatestFrom 时,我正在努力将参数传递给选择器,这是之前从加载操作有效负载映射的
loadLocalSubServices$: Observable<Action> = this.actions$.pipe(
ofType(LocalSubServiceTemplateActions.LocalSubServicesTemplateActionTypes.LoadLocalSubService),
map((action: LocalSubServiceTemplateActions.LoadLocalSubService) => action.payload.globalSubServiceId),
// and below I would like to pass globalSubServiceId
withLatestFrom(this.store.pipe(select(fromLocalSubservices.getSearchParams(globalSubServiceId)))),
map(searchParams => searchParams[1]),
mergeMap((params) =>
this.subServiceService.getLocalSubServices(params).pipe(
map(localSubServices => (new LocalSubServiceTemplateActions.LocalSubServiceLoadSuccess(localSubServices))),
catchError(err => of(new LocalSubServiceTemplateActions.LocalSubServiceLoadFail(err)))
)
)
);
最佳答案
我想我有你(或 future 的流浪者)正在寻找的食谱。您必须将初始有效负载(下面的 of
运算符)映射到内部可观察对象,以便它可以通过管道传输并作为参数传递给 withLatestFrom
.然后mergeMap
将其展平,您可以将其作为一个数组返回给下一个运算符,并将初始负载作为第一个值。
map(action => action.payload),
mergeMap((id) =>
of(id).pipe(
withLatestFrom(
this.store.pipe(select(state => getEntityById(state, id))),
this.store.pipe(select(state => getWhateverElse(state)))
)
),
(id, latestStoreData) => latestStoreData
),
switchMap(([id, entity, whateverElse]) => callService(entity))
关于angular - NGRX 影响如何将参数传递给 withLatestFrom 操作符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609748/