为什么 action 不会在第二次运行时触发 Effect? [英] 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 时,observable 是完整的并且不会在我第二次分派动作时触发效果.我寻找一种方法来正确处理错误并继续观察流,以便我可以在我的组件中订阅它并采取相应的行动.
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 Effect 中的解决方法没有第二次运行 对我不起作用,或者我无法让它起作用.
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)))
)
)
这篇关于为什么 action 不会在第二次运行时触发 Effect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!