How to access global variable in Angular 2 Component How to access global variable in Angular 2 Component angularjs angularjs

How to access global variable in Angular 2 Component


You have in your code :

    var gapi: any;    console.log(gapi); // undefined

The line var gapi creates a new local variable at runtime. You don't want to create a local variable. You want to declare that one exists globally. Create a globals.d.ts file and add the following to it:

declare var gapi:any;

More

JavaScript to TypeScript Migration guide : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html


You can easily access global variables by referencing window, like so

gapi = window["gapi"];

or

let gapi = window["gapi"];