gpt4 book ai didi

angular - 如何使用 ngrx-router-store 在 ngrx 效果中获取路由参数?

转载 作者:太空狗 更新时间:2023-10-29 17:25:01 28 4
gpt4 key购买 nike

我有效果类,我想根据路由器参数 ID 加载详细信息

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

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

最佳答案

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

export const getRouterState = createFeatureSelector<
fromRouter.RouterReducerState<RouterStateUrl>
>('router');

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

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

关于angular - 如何使用 ngrx-router-store 在 ngrx 效果中获取路由参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com