angular - 在 Angular 中使用 google 登录时出现 attachClickHandler 错误
问题描述
我正在尝试在我的 Angular 项目中使用 google 实现社交登录
这是我的相关登录 html 代码
<button (click)="prepareLoginButton()" #googleSignUp >Login Using Google</button>
这是我相关的 ts 代码
export class LoginComponent implements OnInit {
@ViewChild('googleSignUp', { static: true })
googleSignUp: ElementRef;
auth2: any;
ngOnInit(): void {
this.googleSDK();
}
//google login
prepareLoginButton() {
this.auth2.attachClickHandler(
this.googleSignUp.nativeElement,
{},
(googleUser) => {
let profile = googleUser.getBasicProfile();
this.socialLogin.id = profile.getId();
this.socialLogin.fullName = profile.getName();
this.socialLogin.email = profile.getEmail();
this.socialService.setSocialDetails(this.socialLogin);
},
(error) => {
alert(JSON.stringify(error, undefined, 2));
}
);
}
googleSDK() {
window['googleSDKLoaded'] = () => {
window['gapi'].load('auth2', () => {
this.auth2 = window['gapi'].auth2.init({
client_id: 'xxxxxxxxxxxxx',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.prepareLoginButton();
});
};
(function(d, s, id) {
var js,
fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = 'https://apis.google.com/js/platform.js?onload=googleSDKLoaded';
fjs.parentNode.insertBefore(js, fjs);
})(document, 'script', 'google-jssdk');
}
}
我已经面对这个错误很长时间了
请告诉如何解决它
解决方案
我不知道这是否对您有帮助,但对于您prepareLoginButton
在加载方法中调用的人以及当用户单击按钮时,这可能不是您想要的。
您可以gapi.load
从角度调用
declare const gapi: any; //put this ABOVE your @Component (after imports)
private googleSDK() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: environment.googleClientId,
cookie_policy: 'single_host_origin',
scope: 'profile email',
});
this.prepareLoginButton();
});
}
推荐阅读
- laravel - laravel - 保存到数据库后如何调用存储在控制器中的函数?
- .net-core - c# dotNetCore -- 动态编译代码时出错
- python - Python 正则表达式在 HTML 上查找字符串并写入文本文件
- django - Django3 prefetch_related
- java - 从其他活动返回结果到地图活动
- vb.net - DataTable:根据重复 ID 将行移动到新列
- plsql - 为什么在编译触发时出现错误?
- javascript - 满足条件时选择多个单选按钮
- python - elasticsearch.exceptions.SSLError:ConnectionError 主机名不匹配
- r - 如何将“可反应”表导出到 R 中的图像(PDF、JPG、PNG 或 HTML 页面?)?