[英]import module and boostraping app after promise response
我有一个 promise 它返回配置字段(而不是环境变量),并且只有在收到响应后,应用程序才会被引导。
export class AppModule {
constructor(private configService: ConfigService) {}
ngDoBootstrap(app: ApplicationRef) {
this.configService.init().then((config) => {
app.bootstrap(AppComponent);
});
}
}
问题是在@NgModule()
内部注入的模块具有配置 object 作为依赖项。 所以,我只需要在服务响应后导入它(如在 boostraping 案例中)。
@NgModule({
imports: [
...,
// configuration is load inside of class (below)
StylesComponentsModule.forRoot(configuration) // in old implementation, here was used environment object
]
})
export class AppModule { ... }
是否可以在AppModule
class 中添加导入的模块(如 boostraping 示例)? 像 ngDoImport 这样的东西不起作用......
真正的问题: StylesComponentsModule
是一个内部库,它需要来自父应用程序的环境变量。 因为我们更改了环境文件,在运行时加载了配置 json,所以我们需要在收到configService.init()
的响应后将此配置 object 提供给StylesComponentsModule
。
稍后编辑:
这个解决方案可以工作
export class AppModule {
constructor(private configService: ConfigService) {}
ngDoBootstrap(app: ApplicationRef) {
this.configService.init().then((config) => {
import('module/path')
then(module => module.forRoot(config));
app.bootstrap(AppComponent);
});
}
}
谢谢
正如评论中所解释的,这不是正确的方法。
应该做的是使用 APP_INITIALIZER 令牌:
或者,如果有路由组件,您还可以在需要时使用解析器,这样您就不必每次都加载整个配置(提高 TTI,交互时间,这很重要)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.