Javascript 自定义元素内的googleauth回调
我正试图使用下面的代码将GoogleAuth放入一个自定义元素中。它的呈现正确,并且该按钮会触发常规的google auth弹出窗口,但在完成登录后,不会触发回调-不会触发任何日志,也不会出现错误消息。有什么建议吗 我猜这与我正在使用一个类有关,因为我在某处读到字符串需要引用一个全局函数。但在这种情况下,这当然是不可能的Javascript 自定义元素内的googleauth回调,javascript,google-authentication,custom-element,Javascript,Google Authentication,Custom Element,我正试图使用下面的代码将GoogleAuth放入一个自定义元素中。它的呈现正确,并且该按钮会触发常规的google auth弹出窗口,但在完成登录后,不会触发回调-不会触发任何日志,也不会出现错误消息。有什么建议吗 我猜这与我正在使用一个类有关,因为我在某处读到字符串需要引用一个全局函数。但在这种情况下,这当然是不可能的 customElements.define( "google-auth", class extends HTMLElement { constr
customElements.define(
"google-auth",
class extends HTMLElement {
constructor() {
super();
this._profile = {};
}
onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log("Name: " + profile.getName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail()); // This is null if the 'email' scope is not present.
this._profile = profile;
};
connectedCallback() {
console.log("google-auth");
this.innerHTML = `
<div class="g-signin2" data-onsuccess="onSignIn"></div>
`;
}
}
);
customElements.define(
“谷歌认证”,
类扩展HtmleElement{
构造函数(){
超级();
这个._profile={};
}
onSignIn(谷歌用户){
var profile=googleUser.getBasicProfile();
console.log(“ID:+profile.getId());//不要发送到后端!改用ID令牌。
log(“Name:+profile.getName());
log(“图像URL:+profile.getImageUrl());
console.log(“Email:+profile.getEmail());//如果“Email”作用域不存在,则为null。
这个。_profile=profile;
};
connectedCallback(){
log(“googleauth”);
this.innerHTML=`
`;
}
}
);
您还可以按照上的Google Auth文档中的说明明确定义成功回调
例如:
connectedCallback() {
console.log("google-auth");
this.innerHTML = `
<div id="my-signin2"></div>
`;
gapi.signin2.render('my-signin2', {
'theme': 'dark',
'onsuccess' : this.onSignIn.bind(this)
});
}
您是否尝试在connectedCallback方法上定义window.onSignIn=this.onSignIn?和
window.onSignIn=this.onSignIn.bind(this)
甚至更好,我也发现了这一点,但在您提供该解决方案时,在注释中恢复为您的解决方案。无论如何,我很感激
function render() {
GA.render() // where GA is the custom element id
}
class extends HTMLElement {
render() {
gapi.signin2.render('my-signin2',
...
}
}