NGRX影响如何将参数传递给withLatestFrom运算符 [英] NGRX Effects how to pass parameter to withLatestFrom operator

查看:58
本文介绍了NGRX影响如何将参数传递给withLatestFrom运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用withLatestFrom时,我正在努力将参数传递给选择器,它是从加载动作有效负载中映射的

I am struggling with passing parameter to selector when by using withLatestFrom, which was mapped earlier from load action payload

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)))
    )
  )
);

推荐答案

我认为我有您(或未来的流浪者)正在寻找的食谱.您必须将初始有效负载(下面的of运算符)映射到内部可观察对象,以便可以通过管道将其作为参数传递给withLatestFrom.然后mergeMap将其展平,您可以将其作为一个数组返回给下一个运算符,并以初始有效载荷作为第一个值.

I think I have the recipe you (or future wanderers) are looking for. You have to map the initial payload (of operator below) to an inner observable so that it can be piped and passed as a param to withLatestFrom. Then mergeMap will flatten it and you can return it to the next operator as one array with the initial payload as the first value.

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))

这篇关于NGRX影响如何将参数传递给withLatestFrom运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆