Я новичок в angular 2, поэтому постараюсь подробно объяснить требование. Приложение, которое я создаю, имеет страницу входа (/login) и страницу настроек (/settings).
когда пользователь получает доступ к странице входа в систему, переменная gapi инициализируется правильно, после чего пользователь входит в приложение.
Как только пользователь входит, у него есть страница настроек. проблема начинается, когда пользователь обновляет страницу, когда это происходит, переменная gapi больше не распознается и становится неопределенной. Я думаю, что библиотека gapi не загружается и, следовательно, не работает.
Я поместил следующий код в файл index.html приложения.
<script type="text/javascript">
// Client ID and API key from the Developer Console
var CLIENT_ID = '***.apps.googleusercontent.com';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/gmail.readonly';
/**
* On load, called to load the auth2 library and API client library.
*/
function handleClientLoad() {
console.log("handleClientLoad")
gapi.load('client:auth2', initClient);
}
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
function initClient() {
gapi.client.init({
discoveryDocs: DISCOVERY_DOCS,
clientId: CLIENT_ID,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
console.log("client init");
gapi.auth2.getAuthInstance().isSignedIn.listen();
// Handle the initial sign-in state.
//updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload=this.onload=function(){};handleClientLoad();
onreadystatechange="if (this.readyState === 'complete') this.onload()";>
</script>
В заключение, как я могу правильно загрузить модуль gapi для обработки описанного выше сценария обновления?
Я пытался работать с решением из Лучший способ дождаться завершения инициализации сторонней JS-библиотеки в службе Angular 2? однако это не сработало, gapi по-прежнему не определен.