我们目前有一个问题,我们想动态创建一个模块(moduleFactory.create(this.injector))。但是动态创建的模块的注入器没有来自父注入器的提供者。我们希望在我们的集成/主应用程序(创建子模块的地方)中实现一些核心内容,将其放入根注入器,并希望通过 DI(通过注入器,由主注入器创建)将其放入我们的子模块和组件中应用程序的注入器作为父级)。
我们的实现基于这个例子:https : //stackblitz.com/edit/angular-ifs7sp?file=src%2Fapp%2Flazy-loader.service.ts
const subsystems = await this.fetchSubsystems()
const promises = subsystems
.map(async (subsystem) => await subsystem.load())
const subsystemModuleFactories = await Promise.all(promisses)
subsystemModuleFactories.map(async (ngModuleOrNgModuleFactory) => {
let moduleFactory: NgModuleFactory<any>
if (ngModuleOrNgModuleFactory instanceof NgModuleFactory) {
moduleFactory = ngModuleOrNgModuleFactory
} else {
const compiler = this.injector.get(Compiler)
moduleFactory = await compiler.compileModuleAsync(ngModuleOrNgModuleFactory)
}
const entryComponent = (<any>moduleFactory.moduleType).entry
const moduleRef = moduleFactory.create(this.injector) // create the module ref with the injector of the main app as parent of the subsystem module …Run Code Online (Sandbox Code Playgroud)