无法在 Angular 模块中加载动态参数 forRoot import while 运行 --aot flag
Can't load dynamic arguments in an Angular module forRoot import while running with --aot flag
我试图让我的代码在 --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 将动态数据加载到我的单例服务中?
您应该导出 LoggingService 和 StoreService 的 useFactory
函数,因为,我引用了 trichetriche
You simply can't use function calls in decorators
日志服务
export function loggingFactory(conf: AppConfiguration) {
return conf.Application.defaultLogType === 'console' ?
new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}
{
provide: LoggingService,
useFactory: loggingFactory,
deps:[APP_CONFIG]
}
商店服务:
export function storeFactory(conf: AppConfiguration) {
return new StoreService(conf);
}
{
provide: StoreService,
useFactory: storeFactory,
deps: [APP_CONFIG]
},
但是这个似乎有点过时了,因为你可以直接使用该服务并使用 @Inject()
注入 APP_CONFIG
@Injectable()
export class StoreService {
constructor(@Inject(APP_CONFIG) appConfig: AppConfiguration) {}
}
我试图让我的代码在 --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 将动态数据加载到我的单例服务中?
您应该导出 LoggingService 和 StoreService 的 useFactory
函数,因为,我引用了 trichetriche
You simply can't use function calls in decorators
日志服务
export function loggingFactory(conf: AppConfiguration) {
return conf.Application.defaultLogType === 'console' ?
new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}
{
provide: LoggingService,
useFactory: loggingFactory,
deps:[APP_CONFIG]
}
商店服务:
export function storeFactory(conf: AppConfiguration) {
return new StoreService(conf);
}
{
provide: StoreService,
useFactory: storeFactory,
deps: [APP_CONFIG]
},
但是这个似乎有点过时了,因为你可以直接使用该服务并使用 @Inject()
APP_CONFIG
@Injectable()
export class StoreService {
constructor(@Inject(APP_CONFIG) appConfig: AppConfiguration) {}
}