@ngrx效果不会第二次运行 [英] @ngrx Effect does not run the second time

查看:131
本文介绍了@ngrx效果不会第二次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了解@ ngrx/store和@ ngrx.effects,并在Angular/Ionic应用程序中创建了我的第一个效果.第一次运行正常,但是如果我再次将事件调度到商店(即再次单击按钮时),则什么都不会发生(不会进行网络调用,控制台日志中也不会发生).有什么明显的地方我做错了吗?效果如下:

I've just started learning about @ngrx/store and @ngrx.effects and have created my first effect in my Angular/Ionic app. It runs ok the first time but if I dispatch the event to the store again (i.e when clicking the button again), nothing happens (no network call is made, nothing in console logs). Is there something obvious I'm doing wrong? Here's the effect:

@Effect() event_response$ = this.action$
    .ofType(SEND_EVENT_RESPONSE_ACTION)
    .map(toPayload)
    .switchMap((payload) => this.myService.eventResponse(payload.eventId,payload.response))
    .map(data => new SentEventResponseAction(data))
    .catch((error) => Observable.of(new ErrorOccurredAction(error)));

谢谢

推荐答案

听起来好像正在发生错误.在这种情况下,catch返回的observable中的动作将被发射到效果的流中,然后效果将完成-这将防止在发出错误动作后效果继续运行.

It sounds like an error is occurring. In that situation, the action in the observable returned by catch will be emitted into the effect's stream and the effect will then complete - which will prevent the effect from running after the error action is emitted.

mapcatch移至switchMap:

@Effect() event_response$ = this.action$
  .ofType(SEND_EVENT_RESPONSE_ACTION)
  .map(toPayload)
  .switchMap((payload) => this.myService
    .eventResponse(payload.eventId, payload.response)
    .map(data => new SentEventResponseAction(data))
    .catch((error) => Observable.of(new ErrorOccurredAction(error)))
);

如果在出现错误的情况下在switchMap中编写catch,将阻止效果完成.

Composing the catch within the switchMap will prevent the effect from completing if an error occurs.

这篇关于@ngrx效果不会第二次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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