我正在尝试在--aot标志期间编译我的代码,但是在尝试将环境配置加载到导入到我的app.module.ts文件中的日志记录服务中时,它失败了。 我有类似“ ng serve”的代码,可以很好地运行
这是我想动态传递到单例日志记录服务的结构。
// In it's own ts file, not in the module
export interface AppConfiguration {
user: User;
application: ApplicationConfig;
}
// In it's own ts file, not in the module
export const appConfig = (): AppConfiguration => {
// ... logic happens here to set up the environment, load cookies etc that will result something like this
const userConf: User = {username: 'ronnrak', name: 'Ronny Raketgevär', id: 1, location: 'a place'};
const appConf: ApplicationConfig = {defaultLogLevel: 'debug', defaultLogType: 'console'};
return {user: userConf, application: appConf};
}
logging.module.ts:
export const APP_CONFIG = new InjectionToken<AppConfiguration>('app_config');
@NgModule({
imports: [CommonModule]
})
export class LoggingModule {
constructor(@Optional() @SkipSelf() parentModule: LoggingModule) {
if(parentModule) {
throw new Error('LoggingModule is already loaded, import in AppModule only.');
}
}
static forRoot(config: AppConfiguration): ModuleWithProviders {
return {
ngModule: LoggingModule,
providers: [
{provide: APP_CONFIG, useValue: config},
{provide: LoggingService, useFactory: (conf: AppConfiguration) => {
return conf.Application.defaultLogType === 'console' ? new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}, deps:[APP_CONFIG]}
]
};
}
}
发生错误的app.module.ts
const applicationConfigData: AppConfiguration = appConfig(); <-- error points here
@NgModule({
declarations: [
...
],
imports: [
...
LoggingModule.forRoot(applicationConfigData),
...
],
providers: [
{provide: APP_CONFIG, useValue: applicationConfigData},
{provide: StoreService, useFactory: (conf: AppConfiguration) => new StoreService(conf), deps: [APP_CONFIG]},
...
],
schemas: [
...
],
bootstrap: [
...
]
})
export class AppModule {}
这是我在运行--aot时遇到的错误
ERROR in apps\project\src\app\app.module.ts(17,49): Error during template compile of 'AppModule'
Function expressions are not supported in decorators in 'appConfig'
'appConfig' references 'appConfig'
'appConfig' contains the error at libs\environment\src\lib\init\app-init.ts(19,26)
Consider changing the function expression into an exported function.
我尝试不带lambda表达式而只是更改了清晰的导出函数来更改appConfig,但是它也不起作用。
ERROR in apps\project\src\app\app.module.ts(17,49): Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'appConfig' was called.
如何从app.module将动态数据加载到我的单例服务中?
答案 0 :(得分:2)
您应该导出LoggingService和StoreService的useFactory
函数,因为我引用了trichetriche
您根本无法在装饰器中使用函数调用
LoggingService
export function loggingFactory(conf: AppConfiguration) {
return conf.Application.defaultLogType === 'console' ?
new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}
{
provide: LoggingService,
useFactory: loggingFactory,
deps:[APP_CONFIG]
}
还有StoreService:
export function storeFactory(conf: AppConfiguration) {
return new StoreService(conf);
}
{
provide: StoreService,
useFactory: storeFactory,
deps: [APP_CONFIG]
},
但是这个似乎有点过时了,因为您可以直接使用服务并使用APP_CONFIG
注入@Inject()
@Injectable()
export class StoreService {
constructor(@Inject(APP_CONFIG) appConfig: AppConfiguration) {}
}