4 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
當(dāng)我使用 HTML API 方法時(shí),我遇到了類似的問題,所以我最終使用了JavaScript API。
這就是我所做的:
首先,確保安裝@types/google-one-tap軟件包。
正如您所提到的,我還將腳本導(dǎo)入到我的index.html
文件中,如下所示:
<body>? ?<script?src="https://accounts.google.com/gsi/client"?async?defer></script> ??<app-root> ??</app-root></body>
現(xiàn)在,繼續(xù)討論您的主要組件,在我的例子中是app.component.ts
,首先導(dǎo)入以下內(nèi)容:
import { CredentialResponse, PromptMomentNotification } from 'google-one-tap';
然后,您可以將其添加到ngOnInit()
.?請務(wù)必閱讀文檔以獲取有關(guān)onGoogleLibraryLoad事件的更多詳細(xì)信息:
// @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
函數(shù)是您使用用戶憑據(jù)處理實(shí)際響應(yīng)的地方。就我而言,我想先對其進(jìn)行解碼。查看此內(nèi)容以獲取有關(guān)憑證解碼后的外觀的更多詳細(xì)信息: 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貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
設(shè)置模板中的div在ngOnInit中渲染
`<div id="loginBtn" > </div>`
在您的login.ts中動(dòng)態(tài)注入腳本標(biāo)簽,如下所示
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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
我在將函數(shù)添加到角度分量時(shí)也遇到了同樣的問題。然后我找到了一個(gè)解決方案,通過添加 JS 函數(shù),appComponent如下所示:
(window as any).handleCredentialResponse = (response) => {
/* your code here for handling response.credential */
}

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