4 回答

TA貢獻1895條經驗 獲得超7個贊
當我使用 HTML API 方法時,我遇到了類似的問題,所以我最終使用了JavaScript API。
這就是我所做的:
首先,確保安裝@types/google-one-tap軟件包。
正如您所提到的,我還將腳本導入到我的index.html
文件中,如下所示:
<body>? ?<script?src="https://accounts.google.com/gsi/client"?async?defer></script> ??<app-root> ??</app-root></body>
現在,繼續討論您的主要組件,在我的例子中是app.component.ts
,首先導入以下內容:
import { CredentialResponse, PromptMomentNotification } from 'google-one-tap';
然后,您可以將其添加到ngOnInit()
.?請務必閱讀文檔以獲取有關onGoogleLibraryLoad事件的更多詳細信息:
// @ts-ignore
window.onGoogleLibraryLoad = () => {
? console.log('Google\'s One-tap sign in script loaded!');
? // @ts-ignore
? google.accounts.id.initialize({
? ? // Ref: https://developers.google.com/identity/gsi/web/reference/js-reference#IdConfiguration
? ? client_id: 'XXXXXXXX',
? ? callback: this.handleCredentialResponse.bind(this), // Whatever function you want to trigger...
? ? auto_select: true,
? ? cancel_on_tap_outside: false
? });
? // OPTIONAL: In my case I want to redirect the user to an specific path.
? // @ts-ignore
? google.accounts.id.prompt((notification: PromptMomentNotification) => {
? ? console.log('Google prompt event triggered...');
? ? if (notification.getDismissedReason() === 'credential_returned') {
? ? ? this.ngZone.run(() => {
? ? ? ? this.router.navigate(['myapp/somewhere'], { replaceUrl: true });
? ? ? ? console.log('Welcome back!');
? ? ? });
? ? }
? });
};
然后,該handleCredentialResponse
函數是您使用用戶憑據處理實際響應的地方。就我而言,我想先對其進行解碼。查看此內容以獲取有關憑證解碼后的外觀的更多詳細信息: https:?//developers.google.com/identity/gsi/web/reference/js-reference#credential
// @ts-ignore
window.onGoogleLibraryLoad = () => {
? console.log('Google\'s One-tap sign in script loaded!');
? // @ts-ignore
? google.accounts.id.initialize({
? ? // Ref: https://developers.google.com/identity/gsi/web/reference/js-reference#IdConfiguration
? ? client_id: 'XXXXXXXX',
? ? callback: this.handleCredentialResponse.bind(this), // Whatever function you want to trigger...
? ? auto_select: true,
? ? cancel_on_tap_outside: false
? });
? // OPTIONAL: In my case I want to redirect the user to an specific path.
? // @ts-ignore
? google.accounts.id.prompt((notification: PromptMomentNotification) => {
? ? console.log('Google prompt event triggered...');
? ? if (notification.getDismissedReason() === 'credential_returned') {
? ? ? this.ngZone.run(() => {
? ? ? ? this.router.navigate(['myapp/somewhere'], { replaceUrl: true });
? ? ? ? console.log('Welcome back!');
? ? ? });
? ? }
? });
};

TA貢獻1812條經驗 獲得超5個贊
設置模板中的div在ngOnInit中渲染
`<div id="loginBtn" > </div>`
在您的login.ts中動態注入腳本標簽,如下所示
constructor(private _renderer2: Renderer2, @Inject(DOCUMENT) private _document: Document){}
ngAfterViewInit() {
const script1 = this._renderer2.createElement('script');
script1.src = `https://accounts.google.com/gsi/client`;
script1.async = `true`;
script1.defer = `true`;
this._renderer2.appendChild(this._document.body, script1);
}
ngOnInit(): void {
// @ts-ignore
window.onGoogleLibraryLoad = () => {
// @ts-ignore
google.accounts.id.initialize({
client_id: '335422918527-fd2d9vpim8fpvbcgbv19aiv98hjmo7c5.apps.googleusercontent.com',
callback: this.googleResponse.bind(this),
auto_select: false,
cancel_on_tap_outside: true,
})
// @ts-ignore
google.accounts!.id.renderButton( document!.getElementById('loginBtn')!, { theme: 'outline', size: 'large', width: 200 } )
// @ts-ignore
google.accounts.id.prompt();
}
}
async googleResponse(response: google.CredentialResponse) {
// your logic goes here
}

TA貢獻1829條經驗 獲得超9個贊
我在將函數添加到角度分量時也遇到了同樣的問題。然后我找到了一個解決方案,通過添加 JS 函數,appComponent如下所示:
(window as any).handleCredentialResponse = (response) => {
/* your code here for handling response.credential */
}

TA貢獻1829條經驗 獲得超6個贊
Google One Tap js 庫嘗試在全局范圍內查找callback
,但找不到它,因為您的callback
函數的范圍位于應用程序內部的某個位置,因此您可以將回調附加到 window, like window.callback = function(data) {...}
。另外,由于您將其附加到窗口,因此最好為該函數指定一個不太通用的名稱。
添加回答
舉報