Observable with rx broken after error

前端 未结 2 868
陌清茗
陌清茗 2021-01-12 23:55

Im trying to write login flow for my app using ngrx store + ng effects. I have managed to write it and it works in happy scenerio, but when user inputs wrong values to the f

2条回答
  •  不知归路
    2021-01-13 00:21

    When an error is caught, the observable returned by catch is used to continue the chain. And the returned observable completes, which completes the effect and sees ngrx unsubscribe from the effect.

    Move the map and catch into the switchMap:

    @Effect() login = this.actions
    .ofType(LoginActions.ATTEMPT_LOGIN)
    .map(toPayload)
    .switchMap(payload => this.loginService.attemptLogin(payload)
      .map(response => new LoginActions.LoginSuccess(response))
      .catch(error => of(new LoginActions.LoginFailed(error)))
    );
    

    Catching inside the switchMap won't complete the effect.

提交回复
热议问题