【发布时间】:2018-07-22 16:15:26
【问题描述】:
我正在尝试使用下面的代码将 google auth 放入自定义元素中。它的渲染正确,并且该按钮会导致常规的 google auth 弹出窗口触发,但在完成登录后,不会触发回调 - 没有触发任何日志,也没有错误消息。有什么建议吗?
我的猜测是它与我使用类的事实有关,因为我在某处读到字符串需要引用全局函数。但在这种情况下,这当然是不可能的
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>
`;
}
}
);
【问题讨论】:
-
您是否尝试在 connectedCallback 方法上定义 window.onSignIn = this.onSignIn?
-
和
window.onSignIn = this.onSignIn.bind(this);更好
标签: javascript google-authentication custom-element