【发布时间】:2018-03-22 20:16:54
【问题描述】:
我正在尝试在应用程序启动之前以角度读取配置文件。我遇到了一个解决方案,但是当我第一次使用 commang ng serve 时,我一直收到错误消息。然后我重新保存代码,webpack 编译成功,没有错误。并且代码工作正常。 错误是
ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 117:45 in the original .ts file), resolving symbol AppModule in C:/Repo/rvm-web-ui/AdaRvmWebClient/src/app/app.module.ts
我添加到 appmodule.ts 的行
AppConfig,
{ provide: APP_INITIALIZER, useFactory: (config: AppConfig) => () => config.load(), deps: [AppConfig], multi: true },
还有appconfig文件
import { Inject, Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { ConnectionHelper } from 'app/config/connections';
@Injectable()
export class AppConfig {
private config: Object = null;
private env: Object = null;
constructor(private http: Http) {
}
/**
* Use to get the data found in the second file (config file)
*/
public getConfig(key: any) {
return this.config[key];
}
/**
* Use to get the data found in the first file (env file)
*/
public getEnv(key: any) {
return this.env[key];
}
/**
* This method:
* a) Loads "env.json" to get the current working environment (e.g.: 'production', 'development')
* b) Loads "config.[env].json" to get all env's variables (e.g.: 'config.development.json')
*/
public load() {
return new Promise((resolve, reject) => {
this.http.get('conf/env.json').map( res => res.json() ).catch((error: any):any => {
console.log('Configuration file "env.json" could not be read');
resolve(true);
return Observable.throw(error.json().error || 'Server error');
}).subscribe( (envResponse:any) => {
this.env = envResponse;
let request:any = null;
switch (envResponse.env) {
case 'production': {
request = this.http.get('conf/config.' + envResponse.env + '.json');
} break;
case 'development': {
request = this.http.get('conf/config.' + envResponse.env + '.json');
} break;
case 'default': {
console.error('Environment file is not set or invalid');
resolve(true);
} break;
}
if (request) {
request
.map( res => res.json() )
.catch((error: any) => {
console.error('Error reading ' + envResponse.env + ' configuration file');
resolve(error);
return Observable.throw(error.json().error || 'Server error');
})
.subscribe((responseData:any) => {
ConnectionHelper.SERVER_IP = responseData.server;
ConnectionHelper.SERVER_PORT = responseData.port;
ConnectionHelper.SERVER_URL = ConnectionHelper.SERVER_IP+":"+ConnectionHelper.SERVER_PORT+ConnectionHelper.ADA_URL;
resolve(true);
});
} else {
console.error('Env config file "env.json" is not valid');
resolve(true);
}
});
});
}
}
【问题讨论】:
标签: angular dependency-injection provider