I have a small angular application that makes use of the CoreModule
and SharedModule
pattern. Because
my application is quite small at the moment, I have imported the SharedModule
in the root module, AppModule
. I also have a lazy loaded admin module called AdminModule
but it seems like it does not have access to shared module even though it has been imported in the root module. I have to explicitly import the SharedModule
again in order to use it inside the components of the admin module. Why is this? If I have to re-import them again inside each lazy loaded module, what's the point of importing them in the root module?
AppModule
...
imports: [
BrowserModule,
BrowserAnimationsModule,
MatExpansionModule,
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule, // imports firebase/firestore, only needed for database features
AngularFireAuthModule, // imports firebase/auth, only needed for auth features,
AngularFireStorageModule, // imports firebase/storage only needed for storage features
SharedModule, // To be imported on each feature module, instead of AppModule. For now, this is fine though
CoreModule,
HttpClientModule
],
...
App routing module (Lazy loaded)
const routes: Routes = [
{
path: 'admin',
loadChildren: './admin/admin.module#AdminModule',
canActivate: [AdminAuthGuard]
}]
AdminModule
...
imports: [CommonModule, AdminRoutingModule, SharedModule, CoreModule],
An example is if I have a footer component inside the SharedModule
and try to use it in my AdminHomeComponent
which is a part of the AdminModule
, I get an error (without importing the SharedModule
). All works fine when I import it.