亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Angular 中使用 Google One Tap

在 Angular 中使用 Google One Tap

繁花不似錦 2024-01-18 16:16:22
我想在我的 Angular 11 應用程序中使用 Google One Tap。按照文檔,我添加<script async defer src="https://accounts.google.com/gsi/client"></script>到我的 html 中,然后在我的 中使用以下代碼app.component.html:<div id="g_id_onload"     data-client_id="MY_GOOGLE_CLIENT_ID"     data-callback="handleCredentialResponse",     data-cancel_on_tap_outside="false"> </div>彈出窗口工作正常,但我似乎無法登錄。如果我handleCredentialResponse在 中創建一個函數app.component.ts,則會收到以下錯誤:[GSI_LOGGER]: The value of 'callback' is not a function. Configuration ignored.如果我嘗試使用 JavaScript API,Typescript 會拋出以下錯誤:Property 'accounts' does not exist on type 'typeof google'我應該怎么做才能在 Angular 中使用 Google One Tap?
查看完整描述

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!');

? ? ? });

? ? }

? });

};


查看完整回答
反對 回復 2024-01-18
?
ABOUTYOU

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

}


查看完整回答
反對 回復 2024-01-18
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

我在將函數添加到角度分量時也遇到了同樣的問題。然后我找到了一個解決方案,通過添加 JS 函數,appComponent如下所示:


(window as any).handleCredentialResponse = (response) => {

      /* your code here for handling response.credential */

}


查看完整回答
反對 回復 2024-01-18
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

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



查看完整回答
反對 回復 2024-01-18
  • 4 回答
  • 0 關注
  • 275 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號