2017-07-24 32 views
0

我正在運行此代碼,有時候我得到一個錯誤,其他人我沒有,我不明白爲什麼。gapi.auth.authorize「無法讀取未定義的屬性」授權「

let promiseLogIn = new Promise(function(reject,resolve){ 
    gapi.auth.authorize(authData , function(response) { 
     var authButton = document.getElementById('auth-button'); 
     if (response.error) { 
     console.log("AuthBad"); 
     resolve(); 
     authButton.hidden = false; 
     } 
     else { 
     console.log("AuthGood"); 
     reject(); 
     authButton.hidden = true; 
     } 
    }); 
    }); 

我加載的lib在我的HTML文件中像這樣:

<script src="https://apis.google.com/js/client.js?onload=authorize"></script> 
<script src="sources/scripts/dist/bundle.js"></script> 

其中bundle.j是我擁有所有的編譯的TS文件。

+0

你確定你打算在打字稿下標記嗎? – Aron

+0

你確定你正在加載gapi庫嗎? – Aron

+0

@Aron這是使用typescript構建的,我應該如何正確加載它? –

回答

0

很可能您正在收到此錯誤,因爲您沒有範圍內的auth2庫。要將庫添加到您的谷歌作用域,Google的示例文檔鼓勵使用gapi.load()方法加載新包。所以,你可以這樣做:

let promiseLogIn = new Promise(function(reject,resolve){ 
    gapi.load('auth2', function(){ 
    gapi.auth.authorize(authData , function(response) { 
     var authButton = document.getElementById('auth-button'); 
     if (response.error) { 
     console.log("AuthBad"); 
     resolve(); 
     authButton.hidden = false; 
     } 
     else { 
     console.log("AuthGood"); 
     reject(); 
     authButton.hidden = true; 
     } 
    }); 
    }); 
}); 
相關問題