嗨,我正在制作一个带有角度 5 的登录页面。我的方法如下:
login(context: LoginContext): Observable<number> {
let profileId: number;
this.http.post<AuthRes>(SERVER_API_URL + "api/authenticate", context).subscribe(authRes => {
profileId = authRes.profileId;
this.tokenService.setToken(authRes.token, context.rememberMe);
});
return of(profileId);
}
我的问题是,有时,profileId 在第一个可观察对象完成工作之前返回为可观察的,当我尝试验证用户是否已登录时,我得到了错误的结果。
我想仅在执行我的第一个可观察对象后返回配置文件 ID。
谢谢
login(context: LoginContext): Observable<number> {
return this.http.post<AuthRes>(`${SERVER_API_URL}api/authenticate`, context)
.do(authRes => this.tokenService.setToken(authRes.token, context.rememberMe))
.map(authRes => authRes.profileId);
}