【发布时间】: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;
}
}
这是一种有效的方法还是有更好的方法?
【问题讨论】: