如何检查服务是否已完成,然后运行代码的其余部分?
例如:
我想从API的一些数据是这样的:角度2检查服务是否完成
get_user_info(token: string): Observable<any> {
return this.http.get(this.home + 'api/home/account/excerpt?token=' + token)
.map((response: Response) => {
return response.json().result;
});
}
,我想从get_user_info
订阅数据:
retrieve_user_info() {
this.http.get_user_info(this.back_result.token).subscribe(
(result: sendBackUserInfo) => {
if (result.result == 1) {
this.user_info = result.user;
} else {
console.log(result.messages[0]);
}
}
);
}
但问题是,当我打电话函数retrieve_user_info
,它不立即获取数据。
我该如何解决这个问题?
编辑:
我这是怎么叫retrieve_user_info
:
submit_login_form() {
this.http.login_user(this.username, this.password, this.result.language.code)
.subscribe((result: loginSendBackResult) => {
this.back_result = result;
this.retrieve_user_info();
if (this.back_result.result == 1) {
this.show_modal = false;
console.log(user_info);
router.navigate(['home']);
}
});
}
你不能 “解决这个问题”,这是异步的。无论你需要做什么你的数据,你需要在回调内做到这一点:) – Alex
请显示您实际使用该数据的代码。你把它作为'@Input()'发送给某个组件,或者你对它做了什么? –
@deezg问题已更新 – amirsa00