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

时间:2017-04-03 13:40:30

标签: angular typescript angular-promise

在我的Angular 2应用程序中成功实现gapi客户端之后,我现在遇到一个问题,我的http对象未定义,我不确定原因。

以下是代码:     构造函数(私有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帐户的权限,当我收到回复时,我想将收到的一些数据传递给我的后端服务器。

如果我在"之外使用this.http,那么"然后http方法工作正常,但这会产生另一个问题,其中" auth_code"价值无法识别。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:8)

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

使用箭头功能代替:

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