我刚接触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]));
}
谢谢你的帮助。
您需要使用-
导入它if(window){
Object.assign(__env, window.__env);
}
然后使用-
var myModule = angular.module('app', []);
myModule.constant('__env', __env);