Я пытаюсь создать Google Sign-in в приложении Angular, но постоянно получаю следующую ошибку:
Не удается прочитать свойство 'load' из неопределенного
На самом деле у меня это работало всего час назад, но теперь, похоже, возникла проблема с загрузкой - я почти уверен, что что-то не синхронизировано, но я не уверен, что я могу с этим поделать?
Просто для ясности, мое приложение не загружается на страницу входа с самого начала, я перехожу к нему позже, используя адресную строку в Chrome (не знаю, будет ли это иметь значение).У меня там правильный идентификатор клиента, я просто заменил его для этого вопроса в целях безопасности.
login.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor() { }
public auth2: any;
user: string;
gapi: any;
public googleInit() {
this.gapi.load('auth2', () => {
this.auth2 = this.gapi.auth2.init({
client_id: 'client_id.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.attachSignin(document.getElementById('googleBtn'));
});
}
public attachSignin(element) {
this.auth2.attachClickHandler(element, {},
(googleUser) => {
let profile = googleUser.getBasicProfile();
console.log('Token || ' + googleUser.getAuthResponse().id_token);
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
//YOUR CODE HERE
this.user = "";
}, (error) => {
alert(JSON.stringify(error, undefined, 2));
});
}
ngAfterViewInit(){
this.googleInit();
}
ngOnInit() {
}
}
login.component.html
<div id="googleBtn">Login w/ Google</div>