2017-04-03 62 views
2

在我的Angular 2应用程序中成功实现gapi客户端之后,我现在遇到了一个问题,我的http对象未定义,我不知道为什么。TypeError:无法读取未定义角度的属性'http'2

下面是代码: 构造函数(私人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); 
    } 
    ); 
}); 
} 

基本上就是我做的是从用户请求的权限才能访问他的Gmail帐户,当我有我的回应想将收到的一些数据传递给我的后端服务器。

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

缺少什么我在这里?

+0

你有一个函数:'函数(RESP)'这是改变你的'这是你期望的课程。使用箭头功能。 – Maxime

回答

5

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

使用,而不是箭头功能:

then((resp) => { 
相关问题