我正在尝试在应用启动之前以角度读取配置文件。我遇到了一个解决方案,但是当我第一次使用commang ng服务时,我仍然遇到错误。然后我重新保存代码和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);
}
});
});
}
}
答案 0 :(得分:0)
看起来不推荐使用函数定义或lamda函数。 我需要从app模块中提取函数,然后在AppConfig.ts中定义它(可以在任何文件中定义)
export function initConfig(config: AppConfig){
return () => config.load()
}
然后更改了应用模块
AppConfig,
{ provide: APP_INITIALIZER, useFactory: initConfig, deps: [AppConfig], multi: true },
我希望这有助于某人