【问题标题】:RxJS- Angular2 - Conditional streamRxJS- A​​ngular2 - 条件流
【发布时间】:2017-03-30 06:09:31
【问题描述】:

RxJS 新手,使用 Angular2 & @ngrx/store & /effects。

我正在尝试研究如何制作一个处理来自服务的条件结果的流...这是通过检查身份验证凭据是否在应用程序首次运行时确定用户是否登录存储在 LocalStorage 中。

这可能是处理这个问题的完全错误的方法,所以任何建议都值得赞赏。

此刻我有效果

  @Effect() check$ = this.updates$
.whenAction(AuthActions.AUTHENTICATE_CHECK)
.map(update => this.authService.checkAuth())
// now what?  
// returns an observable of false, or a Auth object if LS info found
// needs to return  
// this.authActions.authenticateCheckResult(res) if ok and
//  this.authActions.authenticateCheckError() if not ok

authService中的checkAuth方法是这样的……

checkAuth():Observable {
 this._user = <AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY);

 if(this._user && this._user.accessToken) {
   return Observable.of(this._user);
 }
 else {
   return Observable.of(false);
 }
}

更新

我正在做这个......

  @Effect() check$ = this.updates$
.whenAction(AuthActions.AUTHENTICATE_CHECK)
.map(update => this.authService.checkAuth())
.map(result => {
  if(!result) {
    return this.authActions.authenticateCheckError(false);
  } else {
    return this.authActions.authenticateCheckSuccess(result)
  }
});

//

checkAuth():AuthAPI {
this._user = <AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY);

if(this._user && this._user.accessToken) {
  return this._user;
}
else {
  return null;
}

}

这是一种有效的方法还是有更好的方法?

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    你也可以像

    一样链接你的调用
    checkAuth():Observable<AuthAPI> {
        return Rx.Observable.of(<AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY))
                            .filter(user => user)
                            .filter(user.accessToken)
                            .defaultIfEmpty(null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2019-09-21
      • 2018-10-17
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多