第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 Angular 中使用 Google One Tap

在 Angular 中使用 Google One Tap

繁花不似錦 2024-01-18 16:16:22
我想在我的 Angular 11 應(yīng)用程序中使用 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在 中創(chuàng)建一個(gè)函數(shù)app.component.ts,則會(huì)收到以下錯(cuò)誤:[GSI_LOGGER]: The value of 'callback' is not a function. Configuration ignored.如果我嘗試使用 JavaScript API,Typescript 會(huì)拋出以下錯(cuò)誤:Property 'accounts' does not exist on type 'typeof google'我應(yīng)該怎么做才能在 Angular 中使用 Google One Tap?
查看完整描述

4 回答

?
人到中年有點(diǎn)甜

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

? ? ? });

? ? }

? });

};


查看完整回答
反對 回復(fù) 2024-01-18
?
ABOUTYOU

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

}


查看完整回答
反對 回復(fù) 2024-01-18
?
PIPIONE

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 */

}


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

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è)不太通用的名稱。



查看完整回答
反對 回復(fù) 2024-01-18
  • 4 回答
  • 0 關(guān)注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)