所以,我试图解决这个问题。但是,不知何故我无法这样做,可能是因为缺乏角度 5 的知识。这是我的服务:
GetCurrentUserData(): Observable<ResponseData> {
return this.http.get<ResponseData>(ApplicationURLS.GetCurrentUserInformation)
.map(response => {
return response;
});
//.catch(error => this.handleError(error));
}
这是我的组件:
public GetCurrentUserInformation(): any {
return this.loginService.GetCurrentUserData().subscribe(data => { return data; });
}
在这里,我正在尝试访问数据:
ngAfterViewInit() {
debugger;
this.responseData = this.GetCurrentUserInformation();
if (this.responseData.code != responseCodes.success) {
this.googleInit();
}
}
当我检查 this.responseData 它总是返回这个而不是我想要数据:
我只想进行同步调用,以便立即获取数据。
我也尝试在服务中使用 do() 但它返回的 do() 不是一个函数。
原文由 Just code 发布,翻译遵循 CC BY-SA 4.0 许可协议
这可以通过使用
async/await
🔥🔥 来简化ngAfterViewInit