2016-07-29 11 views
10

Próbuję użyć google logowania z angular2 przez następujące pytanie: Google Sign-In for Websites and Angular 2 using Typescriptlogowanie google za pomocą angular2 i maszynopisu - gdzie uzyskać gapi?

Ale ja dostaję błąd:

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) 

Widocznie GAPI nie jest określony - co mogę zrozumieć jak wydaje się być tylko deklarując pusty var.

Mój obecny kod jest jak poniżej:

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(); 
    }); 
} 
} 

Obciążenia szablon w porządku, to jest po prostu gapi bit.

Moje pytanie brzmi: czego mi brakuje? Jak muszę zdefiniować, aby to działało?

Tu jest mój główny kod 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 { } 

Odpowiedz

8

Czy dołączono skryptu Google platforma API?

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

Zobacz this question instrukcje dotyczące czekać na załadowanie skryptu GAPI przed wykonaniem kodu.

+0

Cześć, dziękuję za odpowiedź! Czy możesz być bardziej szczegółowy? JS/TS z kątem jest zdecydowanie słabym punktem dla mnie. Przejrzałem to pytanie i dodałem je do głównego szablonu aplikacji - nie najładniejszego rozwiązania. Nadal nie działa i nie jestem do końca pewien, czy to, co zrobiłem, było słuszne, czy jak to poprawić. – Scironic

+0

[To pytanie] (https://stackoverflow.com/questions/19399419/angular-js-and-google-api-client-js-gapi) zapewnia kilka strategii używania Google API z Angular. Wygląda na to, że napotkasz warunek, w którym Angular próbuje uzyskać dostęp do obiektu 'gapi' przed załadowaniem skryptu' platform.js'. Zamiast dodawać tag '

5

Też miałem ten sam problem w Angular v4.0. Cięcie asynchroniczny odroczyć ze skryptu Google platforma API rozwiązać mój problem

mój problem został jak poniżej, gdy użyłem platforma API Google jak:

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

enter image description here

mi rozwiązać mój problem odrzucając asynchronizację od od Skrypt aplikacji platformy Google jak jak poniżej w moim index.html:

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

Usunięcie _async defer_ w NG 4 rozwiązało mój problem. Dziękujemy! –

0

I rozwiązać problem poprzez wywołanie poniższy skrypt w indeksie.html

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

Uwaga:

Użyj następującego skryptu bez asynchroniczny odroczyć:

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

i używać asynchroniczny odroczyć w tym skrypcie

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