错误后rx被破坏时可观察到

时间:2017-08-05 14:42:04

标签: javascript angular rxjs ngrx-store ngrx-effects

我正在尝试使用ngrx store + ng效果为我的应用编写登录流程。我已经设法编写它并且它在快乐的场景中工作,但是当用户向表单输入错误的值,以便服务器响应401时,下一次登录尝试没有效果。我已经读过,在使用observable时必须捕获异常,以免破坏'流,但据我所知,我抓住了异常,现在仍在使用。

在代码之下;

export class LoginComponent {

  logged = new Observable<any>();

  constructor(private store: Store<AppStore>) {
   this.logged = store.select('login');
  }

  login(username: string, password: string){
    let body = JSON.stringify({username, password});
    this.store.dispatch({type: LoginActions.ATTEMPT_LOGIN, payload: body});
  }

}

@Injectable()
export class LoginEffects {

  constructor(
    private actions: Actions,
    private loginService: LoginService
  ) { }

  @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)));

  @Effect() success = this.actions
    .ofType(LoginActions.LOGIN_SUCCESS)
    .map(toPayload)
    .map(payload => localStorage.setItem(Global.TOKEN, payload))
    .map(() => go(['/menu']));

  @Effect() error = this.actions
    .ofType(LoginActions.LOGIN_FAILED)
    .map(toPayload)
    .map(payload => console.log(payload))
    .ignoreElements();
}

export const login = (state: any = null, action: Actions) => {
  switch (action.type) {
    case LoginActions.ATTEMPT_LOGIN:
      console.log('hello from reducer attempt login');
      return action.payload;
    case LoginActions.LOGIN_SUCCESS:
      console.log('hello from reducer success login');
      return action.payload;
    case LoginActions.LOGIN_FAILED:
      console.log('hello from reducer failed login');
      return action.payload;
    default:
      return state;
  }
};

@Injectable()
export class LoginService {

  auth = new Observable<any>();
  constructor(private store: Store<AppStore>, private http: Http) {
    this.auth = store.select('auth');
  }

  attemptLogin(payload): Observable<any> {
    // let body = JSON.stringify({username, password});
    return this.http.post(Global.API_URL + '/login', payload)
      .map(response => response.headers.get('Authorization'));
      // .map(action => (
      //   { type: LoginActions.ATTEMPT_LOGIN, payload: action}))
      // .subscribe(action => {this.store.dispatch(action);
      // });
  }
}

2 个答案:

答案 0 :(得分:8)

捕获错误时,catch返回的observable用于继续链接。返回的observable完成,完成效果并看到ngrx取消订阅效果。

mapcatch移至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)))
);

switchMap内捕捉不会完成效果。

答案 1 :(得分:0)

快乐之路场景Happy path flow ngrx-effects

错误情况:需要减速器处理错误情况,并在需要时向用户显示一些消息。 Error scenario ngrx-effects

通过使用pipe中的catchErrorrxjs/operators,我们可以将两个(或所有)错误情况重新映射到单个ErrorAction中,然后我们可以直接在{{1 }}并进行ui更改。

effects