【问题标题】:How to access windows environment variables in angular2 JS app如何在 angular2 JS 应用程序中访问 windows 环境变量
【发布时间】:2016-09-15 18:33:12
【问题描述】:

我是 Angular2 的新手。我正在寻找在我的 Angular 2 应用程序中访问 Windows 环境变量的方法。我有一个 env.js 文件,它的 URL 如下。-

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

我需要在我的 angular 2 组件中访问 this 变量,但它给出了类型窗口上不存在 _env 的错误。

 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]));


                }

感谢您的帮助。

【问题讨论】:

    标签: angular


    【解决方案1】:

    您需要使用 - 导入它

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

    然后使用 - 注册它

    var myModule = angular.module('app', []);
    myModule.constant('__env', __env); 
    

    【讨论】:

    猜你喜欢
    • 2018-02-28
    • 2020-02-07
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多