0

I am new to Angular2. I am looking for way to access windows environment variable in my angular 2 app. I have a env.js file which has below URLs.-

(function (window) {
  window.__env = window.__env || {};
   window.__env.previewUrl = 'http://localhost:3000/';
  window.__env.workflowUrl = 'http://localhost:1337';
  window.__env.baseUrl = '/';
}(this));

I need to access the this variable in my angular 2 component but it gives error that _env does not exist on type window.

 this._http.get( window.__env.workflowUrl + this.swaggerDefSplit[1] + queryString1)
            .map(res => res.json())
            .subscribe(data => {
                console.log(data);                
                if (!(data.Results instanceof Array)) {                    
                    // puts a single json object into array for *ngFor
                    data.Results = JSON.parse(JSON.stringify([data.Results]));


                }

Thanks for help.

Priya M
  • 13
  • 1
  • 7

1 Answers1

0

You need to import it using -

if(window){  
  Object.assign(__env, window.__env);
}

And then register it using -

var myModule = angular.module('app', []);
myModule.constant('__env', __env); 
Techidiot
  • 1,921
  • 1
  • 15
  • 28