【发布时间】:2019-10-16 05:50:29
【问题描述】:
我有一个 Angular 应用程序,主视图分为 2 个组件。这将涉及以异步方式处理 NgRx 操作的调度。
1) MenuComponent - 这包含各种导航按钮,例如注销按钮。单击注销按钮后,它会调用authenticationService.logout(),它会向后端发送一个http 请求,然后注销用户。因此,这是一个异步操作。
public logout() {
this.authenticationService.logout();
this.router.navigate(['login']);
}
2) DashboardComponent - 我编写了以下代码来处理 OnDestroy 生命周期钩子,该钩子将在组件被销毁时调用。它使用 NgRx 进行状态管理。
ngOnDestroy() {
this.store.dispatch(new UpdateDashboardConfiguration());
this.store.dispatch(new ClearDashboardState());
}
UpdateDashboardConfiguration() 操作的调度将导致应用程序向服务器发送一个 http 请求以保存仪表板的配置,因此它也是异步的。
主要问题是,当用户通过点击MenuComponent上的注销按钮决定注销时,有没有办法确保UpdateDashboardConfiguration()的调度在ClearDashboardState()动作的调度之前完成,以及来自其他 MenuComponent 的 authenticationService.logout() 被调用?
首选流程如下:
UpdateDashboardConfiguration => ClearDashboardState => logout()
对于那些想知道的人,this.store.dispatch(new UpdateDashboardConfiguration()).subscribe(...) 将不起作用,因为 store.dispatch() 是 void 类型,而不是 observable。
提前谢谢你。
对于那些感兴趣的人,这是 UpdateDashboardConfiguration 操作的Effect。
@Effect()
UpdateDashboardConfiguration$ = this.actions$.pipe(
ofType<UpdateDashboardConfiguration>.(DashboardActionTypes.UpdateDashboardWidget),
withLatestFrom(this.store.select(selectDashboardLayoutStateConfig)),
switchMap(action => {
return this.dashboardService.updateDashboardConfiguration(action).pipe(
map(data => (new UpdateDashboardConfigurationSuccess(data))),
);
}
),
);
【问题讨论】:
标签: angular typescript redux rxjs ngrx