<分区>
我在 Angular4 + PHP 网站上工作,我在其中使用 Promise 向服务器发送 HTTP 请求,因为我希望我的应用程序根据服务器的响应执行路由。我想访问 then() 中的一个类变量,但它抛出 undefined()
错误。
这是我的代码:
status = false;
checkUser(): Promise<any>{
// .....
return this.http
.get('http://localhost:8000/api/user', this.options)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: any) {
data = res.json();
this.status = data.status; // here it throws error undefined
return this.status;
}
还有其他方法可以实现吗?