Created
January 28, 2021 10:53
-
-
Save armanozak/dc5d8a77f816e9754bef46cdb594125d to your computer and use it in GitHub Desktop.
[How to Configure Angular Modules Loaded by the Router] A custom child module factory #blog #angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Compiler, | |
Injector, | |
ModuleWithProviders, | |
NgModuleFactory, | |
NgModuleRef, | |
StaticProvider, | |
Type | |
} from "@angular/core"; | |
export class ChildModuleFactory<T> extends NgModuleFactory<T> { | |
get moduleType(): Type<T> { | |
return this.moduleWithProviders.ngModule; | |
} | |
constructor(private moduleWithProviders: ModuleWithProviders<T>) { | |
super(); | |
} | |
create(parentInjector: Injector | null): NgModuleRef<T> { | |
const injector = Injector.create({ | |
parent: parentInjector, | |
providers: this.moduleWithProviders.providers as StaticProvider[] | |
}); | |
const compiler = injector.get(Compiler); | |
const factory = compiler.compileModuleSync(this.moduleType); | |
return factory.create(injector); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment