首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ngrx-router-store获取ngrx特效中的路由参数?

如何使用ngrx-router-store获取ngrx特效中的路由参数?
EN

Stack Overflow用户
提问于 2018-01-11 13:35:11
回答 1查看 9.3K关注 0票数 8

我有一个effect类,我想根据路由器参数ID加载详细信息

代码语言:javascript
复制
@Effect()
  getDetails$ = this.actions$.ofType(DetailActions.GET_DETAILS).pipe(
    map(toPayload),
    switchMap(payload => {
      return this.detailService
        .getDetail(payload)//I want router params here in payload
        .pipe(
          map(detail=> new DetailActions.GetDetailSuccess(detail)),
          catchError(error =>
            Observable.of(new DetailActions.GetDetailFail(error))
          )
        );
    })
  );

我想要在有效负载中获得路由器参数,这样我就不必从组件传递有效负载,而是直接从effects类获得它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-07 04:42:39

如果您已经有一个选择器映射到您的应用程序路由器状态:

代码语言:javascript
复制
export const getRouterState = createFeatureSelector<
  fromRouter.RouterReducerState<RouterStateUrl>
>('router');

然后,您可以使用rxjs/operators中的withLatestFrom从路由器状态中获取参数,并可能将它们与操作的有效负载合并,如下所示:

代码语言:javascript
复制
@Effect()
getDetails$ = this.actions$.pipe(
    ofType(DetailActions.GET_DETAILS),
    withLatestFrom(
        this.store.select(fromRoot.getRouterState),
        (action, router) => {
            // do your logic here
            // and return a newPayload:
            return {
                id: router.state.params.id,
                payload: action.payload
            }
        }
    ),
    switchMap(newPayload => {
        return this.detailService
        .getDetail(newPayload)
        .pipe(
            map(detail=> new DetailActions.GetDetailSuccess(detail)),
            catchError(error => Observable.of(new DetailActions.GetDetailFail(error)))
        );
    })
);
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48200372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档