【发布时间】:2021-01-14 09:28:31
【问题描述】:
问题描述 我有一个微调模块,它根据我的根状态下的加载属性显示/隐藏。这被放置在 AppComponent 中。
export interface AppState {
loading: boolean
}
我有多个延迟加载的功能模块,每个模块都通过自己的一组效果获取数据。在执行此操作时,我想在效果开始时将 AppState.loading 更新为 true,然后在效果完成时将其设置回 false。
我该怎么做?
解决方法 作为一种解决方法,我在触发我的功能操作之前调度了根操作中定义的操作(将加载设置为 true),然后功能效果返回一组操作。其中一项操作再次属于根操作(将加载设置为 false)。
service.ts
public getMilestonesAction(q: string) {
this.store.dispatch(AppActions.loadingAction({ loading: true})); // This belongs to root-actions
return this.store.dispatch(CalendarActions.getEntriesAction({ q })); // This belongs to feature-actions
}
effect.ts
getMilestonesEffect$ = createEffect(() => this.action$
.pipe(
ofType(CalendarActions.getEntriesAction),
mergeMap(action => this.calendarService.getMilestones(action.q)
.pipe(
switchMap((data: Milestone[]) => [AppActions.loadingAction({ loading: false }), CalendarActions.getEntriesSuccessAction({ milestones: data })]),
catchError((error: any) => from([AppActions.loadingAction({ loading: false }), CalendarActions.getEntriesFailureAction( { error: this.getErrorMessage(error) })]))
))
));
这是解决这个问题的正确方法吗?
【问题讨论】: