谷歌登录与angular2和打字稿 - 在哪里得到gapi?

时间:2016-07-29 17:25:37

标签: javascript typescript angular gapi

我正在尝试通过以下问题使用带有angular2的谷歌登录:Google Sign-In for Websites and Angular 2 using Typescript

但是我收到了一个错误:

ORIGINAL EXCEPTION: ReferenceError: gapi is not defined
ORIGINAL STACKTRACE:
ReferenceError: gapi is not defined
    at LoginAppComponent.ngAfterViewInit (http://localhost:3000/app/login.component.js:33:9)
    at DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.template.js:46:68)
    at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
    at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
    at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12169:23)
    at DebugAppView.AppView.detectChangesInternal (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12154:18)
    at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
    at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:10397:69)
    at eval (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:9911:88)

显然gapi没有定义 - 我可以理解,因为我似乎只是声明一个空变量。

我目前的代码如下:

import {Component, NgZone} from "@angular/core";

declare var gapi: any;

@Component({
    selector: "login",
    templateUrl: "templates/login-template.html"
})
export class LoginAppComponent {
  googleLoginButtonId = "google-login-button";
  userAuthToken = null;
  userDisplayName = "empty";

  constructor(private _zone: NgZone) {
    console.log(this);
  }

  // Angular hook that allows for interaction with elements inserted by the
  // rendering of a view.
  ngAfterViewInit() {
    // Converts the Google login button stub to an actual button.
    gapi.signin2.render(
      this.googleLoginButtonId,
      {
        "onSuccess": this.onGoogleLoginSuccess,
        "scope": "profile",
        "theme": "dark"
      });
  }

  // Triggered after a user successfully logs in using the Google external
  // login provider.
  onGoogleLoginSuccess = (loggedInUser) => {
    this._zone.run(() => {
        this.userAuthToken = loggedInUser.getAuthResponse().id_token;
        this.userDisplayName = loggedInUser.getBasicProfile().getName();
    });
}
}

模板加载正常,只是gapi位。

所以我的问题是:我错过了什么?我如何定义gapi才能使其正常工作?

这是我的主要app.component代码:

import { Component } from '@angular/core';
import { LoginAppComponent } from './login.component'

@Component({
  selector: 'my-app',
  template: `<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
            <script>
            window.onLoadCallback = function(){
              gapi.auth2.init({
                  client_id: 'filler_text_for_client_id.apps.googleusercontent.com'
                });
            }
            </script>
             <h1>Angular2 P.O.C.</h1>
             <login></login>`,
  directives: [LoginAppComponent]
})
export class AppComponent { }

4 个答案:

答案 0 :(得分:10)

您是否已加入Google平台API脚本?

<script src="https://apis.google.com/js/platform.js"></script>

有关如何在执行代码之前等待加载GAPI脚本的说明,请参阅this question

答案 1 :(得分:6)

我在 Angular v4.0 中也有同样的问题。从 Google平台API脚本中删除 async defer 解决了我的问题

当我使用谷歌平台api时,我的问题就像下面那样:

<script src="https://apis.google.com/js/platform.js" async defer></script>

enter image description here

我通过 Google平台API脚本中的 async defer 解决了我的问题,如下所示 索引。 html

<script src="https://apis.google.com/js/platform.js"></script>

答案 2 :(得分:0)

将以下文件添加到项目结构中以删除错误。

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/gapi/gapi.d.ts

这将使您的项目中可以使用gapi。

答案 3 :(得分:0)

我通过在index.html

中调用以下脚本解决了这个问题
<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>

注意:

使用以下脚本而不使用 async defer

<script src="https://apis.google.com/js/platform."></script> 

并在此脚本中使用 async defer

<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>