当不在ngrx/effects中调度动作时,陷入无限循环

Web*_*urk 9 ngrx angular

我正在使用Angular2和ngrx/store和ngrx/effects进行状态管理.

当某个操作失败时,我想显示一条错误消息,但似乎我无法在一个@Effects()块中执行此任务.请参阅以下内容:

  @Effect() selectOfficeFail$ = this.actions$
   .ofType(SelectOfficeActions.LOAD_FAIL)
   .do(() => {
     alert('Error! No offices found!'); // I keep entering here
  });
Run Code Online (Sandbox Code Playgroud)

当上面的代码运行时,警报会无数次运行,直到浏览器崩溃.似乎@Effect() 必须返回一个新的,dispatch()但我不明白为什么.为什么上面的alert()运行无数次?

编辑:不是SelectOfficeActions.LOAD_FAIL多次派遣.只有一次

ber*_*ndg 17

[更新]现在最好的方法是使用这样的dispatch选项:

@Effect({dispatch: false}) selectOfficeFail$ = this.actions$
    .ofType(SelectOfficeActions.LOAD_FAIL)
    .do(() => {
        alert('Error! No offices found!'); // I keep entering here
    });
Run Code Online (Sandbox Code Playgroud)

这意味着"对此行为做出反应但不发送另一个行动".


car*_*ant 6

问题是do允许操作流过您的效果,然后由商店再次调度操作.你可以用它filter来防止这种情况发生:

@Effect() selectOfficeFail$ = this.actions$
  .ofType(SelectOfficeActions.LOAD_FAIL)
  .do(() => {
    alert('Error! No offices found!'); // I keep entering here
  })
  .filter(() => false); 
Run Code Online (Sandbox Code Playgroud)