为什么动作不会在第二次运行时触发“效果"? [英] Why action doesn't trigger Effect the second time it runs?
问题描述
效果:
@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)))
);
当我返回数据时它可以工作,但是当服务器响应错误(例如404)时,可观察值已完成,并且在我第二次调度动作时不会触发效果.我寻找一种可以正确处理错误并继续观察到的流的方法,以便可以在我的组件中订阅它并采取相应的措施.
It works when I have data returned, but when the server responds with an error - 404 for example, the observable is complete and doesn't trigger the effect the second time I dispatch an action. I looked for a way to handle the error properly and to continue the observable stream so I could subscribe to it in my component and act accordingly.
@ngrx效果中的解决方案第二次未运行不适用于我,或者我无法使其正常工作.
The solution in @ngrx Effect does not run the second time did not work for me, or I couldn't make it work.
推荐答案
您需要根据要求catchError
而不是actions$
.为此,您将需要修改代码,如下所示:
You need to catchError
on request instead of the actions$
. To do this, you will need to modify your code as following:
mergeMap(action =>
this.myService.getDocs().pipe(
map(data => new LoadDocsSuccess(data)),
catchError(error => Observable.of(new LoadDocsFailure(error)))
)
)
这篇关于为什么动作不会在第二次运行时触发“效果"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!