TypeError:无法读取未定义的角度2的属性"http"

use*_*673 3 typescript angular-promise angular

在我的Angular 2应用程序中成功实现gapi客户端后,我现在遇到一个问题,我的http对象未定义,我不知道为什么.

下面是代码:constructor(private http:Http){}

initGmailApi() {

gapi.auth2.getAuthInstance().grantOfflineAccess().then(function(resp) {
    console.log(resp);
    const auth_code = resp.code;

    const body = {'AuthCode': auth_code};
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/startgmail', body,  headers).subscribe(
    (Response) => {
        console.log(Response);
    }
    );
});
}
Run Code Online (Sandbox Code Playgroud)

基本上我正在做的是请求用户允许访问他的Gmail帐户,当我有响应时,我想将收到的一些数据传递给我的后端服务器.

如果我在"then"子句之外使用this.http,那么http方法可以正常工作,但这会产生另一个无法识别"auth_code"值的问题.

我在这里错过了什么?

Gün*_*uer 8

如果要this在回调中引用,请不要使用function() {}

使用替代箭头功能:

then((resp) => {
Run Code Online (Sandbox Code Playgroud)