Kenny Ho
Kenny Ho

Reputation: 423

Using google's api.js in angular service.ts

I'm trying to implement a simple google authentication for my Angular 2 app. Is there a way to implement google's https://apis.google.com/js/api.js script inside of gapi in a service.ts?

I get a compilation error when I try to ng serve it. But when I type some arbitrary spaces which causes node to refresh, it serves up a working site.

I see that var gapi exists inside the api.js script ,but is there a way to extract that variable into ts.

import {Injectable} from '@angular/core';
import * as jQuery from 'jquery';


@Injectable()
export class AuthService {
  testFunction() {
    jQuery.getScript("https://apis.google.com/js/api.js", handleClientLoad);
    var CLIENT_ID = 'SECRET';
    var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"];

    var SCOPES = 'https://www.googleapis.com/auth/youtube.readonly';
    var authorizeButton = document.getElementById('authorize-button');
    var signoutButton = document.getElementById('signout-button');
    var signedInMenu = document.getElementById('signed-in-menu');
    /**
     *  On load, called to load the auth2 library and API client library.
     */
    function handleClientLoad() {
      gapi.load('client:auth2', initClient);
    }

}...more code that also calls gapi...

Upvotes: 2

Views: 751

Answers (1)

santosh singh
santosh singh

Reputation: 28642

declare the gapi variable on the top of the class

declare var gapi:any;

Upvotes: 1

Related Questions