首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular Universal中的谷歌OneTap

Angular Universal中的谷歌OneTap
EN

Stack Overflow用户
提问于 2021-01-03 20:27:48
回答 1查看 175关注 0票数 1

我想在我的Angular Universal应用程序(使用OneTap )中实现谷歌SSR。我使用的是Angular 11,在将应用程序转换为Angular Universal之前,以下脚本正在运行:

代码语言:javascript
复制
initGoogleOneTap() {
  const domain = window.location.hostname;
  google.accounts.id.initialize({
    client_id: environment.GOOGLE_OAUTH_CLIENT_KEY,
    cancel_on_tap_outside: false,
    auto_select: true,
    state_cookie_domain: domain,
    callback: (response: any) => {
      this.oneTapLogIn(response.credential)
        .catch( (error) => {
          console.error('Error logging in with Google One Tap: ', error);
        });
    },
  });

  google.accounts.id.prompt( (notification: any) => {
    if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
      // try next provider if OneTap is not displayed or skipped
    }
  });
}

我在脚本的顶部使用了declare var google: any

我得到的错误是:ERROR ReferenceError: google is not defined

EN

回答 1

Stack Overflow用户

发布于 2021-06-12 10:55:45

首先,通过将以下内容放入您的index.html来加载客户端库

代码语言:javascript
复制
<script src="https://accounts.google.com/gsi/client" async defer></script>

然后,像这样实现您的组件

代码语言:javascript
复制
declare var window: any;

ngOnInit() {
  window.google.accounts.id.initialize({
    client_id: 'YOUR CLIENT ID',
    callback: this.handleGgOneTap.bind(this)
  });
  window.google.accounts.id.prompt();
}

handleGgOneTap(resp) {
  console.log('handleGgOneTap ', resp);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65549839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档