是否可以使以下代码与 AoT 兼容?
interface Config {
someField?: string;
anotherField?: number;
}
@NgModule({})
export class SomeModule {
protected static _config: Config;
public static withConfig(config: Config = {}): ModuleWithProviders {
// the line below fails AoT compilation
SomeModule._config = Object.assign({}, { someField: 'defaultValue' }, config);
return {
ngModule: SomeModule,
providers: []
};
}
}
SomeModule.withConfig()
必须在主模块中导入,并且必须在应用程序启动之前同步设置配置(用于装饰功能),但使用 AoT 编译会抛出:
Angular 编译完成,开始 webpack 打包。
错误:静态解析符号值时遇到错误。调用函数“SomeModule”,不支持函数调用。考虑将函数或 lambda 替换为对导出函数的引用,在 /.../app/app.module.ts 中解析符号 AppModule,在 /../app/app.module.ts 中解析符号 AppModule
有任何想法吗?
编辑:由于评论,这是我在 app.module 中导入模块的方式:
@NgModule({
imports: [
SomeModule.withConfig(), // yeah, just like that or with { someField: '' }
]
})
export class AppModule {}