为什么动作不会在第二次运行时触发“效果"? [英] Why action doesn't trigger Effect the second time it runs?

查看:80
本文介绍了为什么动作不会在第二次运行时触发“效果"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

效果:

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆