2017-04-03 141 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) => { 
相關問題