正如标题中所述,我面临的问题是,只有在这些情况下,我的所有效果才能正常工作:
如果我设法注销并与其他用户登录,则会出现问题.只有每个ngOnInit执行动作调度的人都会被调用.没有效果被解雇.
我有两个模块,app.module和pages.module.第一个只声明未登录用户的组件,例如login/register/reset.第二个仅声明登录用户的组件.
在app.module.ts中我导入了所有的reducers并将其设置为StoreModule.provideStore(reducer).在pages.module.ts,另一个模块中,我导入并激活了类似的效果EffectsModule.run(TestEffects).
使用以下代码仅ngOnInit调用内部日志.效果中的日志永远不会被调用.
test.component.ts:
/** ngrx **/
import * as reducers from '../../reducers';
import * as testActions from './actions/test.actions';
. . .
constructor(private _store: Store<reducers.State>){
this._store.select(reducers.getStuff)
.subscribe(stuff => console.log(stuff));
}
ngOnInit() {
console.log('Dispatching Load Action');
this._store.dispatch(new testActions.LoadAction());
}
Run Code Online (Sandbox Code Playgroud)
test.actions.ts:
import { Action } from '@ngrx/store';
export const ActionTypes = {
LOAD: type('[Test] Load'),
LOAD_SUCCESS: type('[Test] Load Success'),
}; …Run Code Online (Sandbox Code Playgroud) 影响:
@Effect()
loadDocsEffect$ = this.actions$.pipe(
ofType(myActionTypes.LoadDocs),
mergeMap(action => this.myService.getDocs()),
map(data => new LoadDocsSuccess(data)),
catchError(error => Observable.of(new LoadDocsFailure(error)))
);
Run Code Online (Sandbox Code Playgroud)
当我返回数据时它可以工作,但是当服务器响应错误时(例如404),可观察对象已完成,并且在我第二次调度动作时不会触发效果。我寻找一种正确处理错误并继续观察到的流的方法,以便可以在我的组件中订阅它并采取相应的措施。
@ngrx Effect中的解决方案第二次不运行对我不起作用,或者我无法使其工作。
有没有办法throw在 ngrx-effects 流中使用Error 对象而不完成流?
我已经阅读了这些关于为什么通过抛出错误杀死流的很好的答案:
https://github.com/ngrx/platform/issues/646
我的问题是我是否正在实施 AngularErrorHandler来捕获错误,如果我能够将其与 ngrx 效果一起使用。
@Effect()
loginUserEffect: Observable<loginActions.Actions> = this.actions$
.ofType(loginActions.LOGIN_USER)
.map((action: loginActions.LoginUser) => action.payload)
.mergeMap(payload => {
return this.loginService
.authenticate(payload)
.map(response => new loginActions.LoginUserSuccess(response))
.catch((error: HttpErrorResponse) =>
of(new loginActions.LoginUserFailure(error))
)
})
@Effect({ dispatch: false })
loginUserEffectSuccess$ = this.actions$
.ofType(loginActions.LOGIN_USER_SUCCESS)
.do(() => this.router.navigate(['/account-home']))
@Effect({ dispatch: false })
loginUserEffectFailure$ = this.actions$
.ofType(loginActions.LOGIN_USER_FAILURE)
.map((action: loginActions.LoginUserFailure) => {
throw action.payload // Stream completes
})
Run Code Online (Sandbox Code Playgroud)
我想我可以创建一些不涉及抛出任何错误的方法来处理错误,但想确保我需要走那条路,或者是否有办法让两者和平共处。
目前在我实现的班级中ErrorHander,我有这个:
@Injectable() …Run Code Online (Sandbox Code Playgroud)