Reputation: 63739
What is the equivalent of the APP_INITIALIZER
for feature modules in Angular? I want to run some code when my feature module is (lazily) loaded. That code will want some dependencies injected.
I can't seem to find any similar item for modules though. I've used the Search on the Angular docs website ("initializer") but found nothing. I've gone through parts of the modules documentation and used similar search terms in-browser. But I haven't found anything.
How can you run code (depending on shared services via DI) on module load in Angular?
PS. Possibly this is an XY-problem, where "Y" is my question above. At this point I'm intrinsically interested in learning about "Y", but for completeness, my "X" is that I want my feature module to call back to a MenuService
in the shared module to register any menu items that feature module wants to contribute.
Upvotes: 5
Views: 4513
Reputation: 63739
Not sure if it's idiomatic, but I'd answer my question by suggesting to use the module constructor
function for this purpose. So for example:
@NgModule({
imports: [
CommonModule,
ThingsRoutingModule,
],
declarations: [
// Feature module components here
],
})
export class ThingsModule {
constructor(myService: MySharedService) {
myService.doSomething('From lazy loaded feature module!');
}
}
Where MySharedService
thus gets injected.
Upvotes: 6