我正在迁移以使用ngrx 效果,但我遇到了以下问题:由于dispatch
返回void
我不确定如果我的 ngrx 效果(例如 from this.userAccountService.updateFirstName(...
)发生错误,如何防止路由器导航。
从我的组件类:
updateFirstName() {
this.formStatus.submitted = true;
if (this.formStatus.form.valid) {
//Dispatch from the store
this.store.dispatch(new UpdateFirstNameAction(this.formStatus.form.value.firstName));
//Undesired behavior: will navigate even in case of error/failure!
this.router.navigate(['/dashboard/useraccount']);
}
}
从我的效果类:
@Effect()
updateFirstName$: Observable<Action> = this.actions$
.ofType(currentUserAccount.ActionTypes.UPDATE_FIRST_NAME)
.map((action: UpdateFirstNameAction) => action.payload)
.switchMap(firstName =>
this.userAccountService
.updateFirstName(firstName)
.map(() => new UpdateFirstNameSuccessAction(firstName))
//I obviously can't navigate from here
);
我不确定使用路由器从效果中导航是否是一种好习惯(甚至可能)。
如果我的效果发生错误,有人可以建议一个干净的解决方案来防止路由器导航吗?