2016-07-29 66 views
10

我想通過以下這個問題,使用谷歌帳戶登錄的同時angular2:Google Sign-In for Websites and Angular 2 using Typescript谷歌登錄與angular2和打字稿 - 在哪裏得到gapi?

但我發現了一個錯誤:

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沒有定義 - 這我能理解因爲我似乎只聲明一個空的var。

我當前的代碼如下:

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 { } 

回答

8

你已經包括了谷歌平臺API的腳本?

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

有關如何等待GAPI腳本執行代碼之前加載說明,請參閱this question

+0

嗨,謝謝你的迴應!你能更詳細些嗎?有棱角的JS/TS絕對是我的弱點。我經歷了這個問題,並將其添加到我的主應用程序模板 - 而不是最漂亮的解決方案。它仍然沒有工作,但我不完全確定我做的是對的 - 還是如何糾正它。 – Scironic

+0

[這個問題](https://stackoverflow.com/questions/19399419/angular-js-and-google-api-client-js-gapi)提供了一些在Angular中使用Google API的策略。這聽起來像是你遇到了一個條件,在加載'platform.js'腳本之前Angular嘗試訪問'gapi'對象。您可以加載'platform.js'以及Angular核心,而不是將'

5

我也角V4.0有同樣的問題。的切割異步從谷歌平臺的API腳本解決我的問題推遲

我的問題是像下面當我用谷歌平臺的API,如:

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

enter image description here

我解決我的問題通過丟棄異步推遲谷歌平臺的API腳本像下面我的index.html

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

在NG 4中刪除_async defer_解決了我的問題。 謝謝! –

0

我通過調用索引下面的腳本解決了這個問題。HTML

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

注:

使用下面的腳本,而不異步推遲

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

,並使用在這個腳本異步推遲

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