I just started using the feature module + lazily loaded routing part of angular framework, and have a bit of trouble getting used to the nuances. One thing that comes up now and again is how to store the services and interfaces- should they go in the providers array of only the modules that use them? or should some kind of main service module be injected into the root module?
I have understood from various tutorials that services and interfaces should not go in the core module or shared module.
what i was doing before hand:
//service.module.ts
@NgModule({
imports: [],
exports: [],
declarations: [],
providers: [],
})
export class ServicesModule {
static forRoot(): ModuleWithProviders<ServicesModule> {
return {
ngModule: ServicesModule,
providers: [
all the services
provided in our application
]
}
}
}
// then in app.module.ts
imports: [
ServicesModule.forRoot(),
]
so my choices are
- leave set up as-is, have a collective directory called 'services' in app folder, and use constructor DI in components that need a service. likewise with models/interfaces, import them from a directory called 'models'.
- move services into modules that need them and only provide shared services in the ServicesModule, helping with lazy loading of modules.
I guess a different/easier question is, if i move all my services back into the provider array of my app.module, would this simplify things? Seems counter intuitive.