Working with Angular15.
Create a lazy loading modules
app-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', patchMatch: 'full' },
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) },
{ path: 'module1', loadChildren: () => import('./module1/module1.module').then(m => m.FirstModule) },
{ path: 'module2', loadChildren: () => import('./module2/module2.module').then(m => m.SecondModule) },
{ path: 'module3', loadChildren: () => import('./module3/module3.module').then(m => m.ThirdModule) },
{ path: 'module4', loadChildren: () => import('./module4/module4.module').then(m => m.FourthModule) },
];
@NgModule({
imports: [RouterModule.forChild(routes, {useHash:true})]
exports: [RouterModule]
})
export class AppRoutingModule {}
dashboard.module.ts -- Having components dependency with FirstModule, SecondModule, ThirdModule and FourthModule
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent},
];
@NgModule({
declarations: [DashboardComponent, DashboardDetailsComponent]
imports: [RouterModule.forChild(routes), FirstModule, SecondModule, ThirdModule, FourthModule]
exports: [RouterModule],
bootstrap: [DashboardComponent]
})
export class DashboardModule {}
module1.module.ts -- Having components dependency with SecondModule and ThirdModule
const routes: Routes = [
{ path: 'module1', component: Module1Component},
{ path: 'module1/details', component: Module1DetailsComponent},
];
@NgModule({
declarations: [Module1Component, Module1DetailsComponent]
imports: [RouterModule.forChild(routes), SecondModule, ThirdModule]
exports: [RouterModule],
bootstrap: [Module1Component]
})
export class FirstModule {}
module2.module.ts -- Having components dependency with ThirdModule
const routes: Routes = [
{ path: 'module2', component: Module2Component},
{ path: 'module2/details', component: Module2DetailsComponent},
];
@NgModule({
declarations: [Module2Component, Module2DetailsComponent]
imports: [RouterModule.forChild(routes), ThirdModule]
exports: [RouterModule],
bootstrap: [Module2Component]
})
export class SecondModule {}
With the above code getting error Error: NG04007: The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector. Lazy load modules hould use RouterModule.forChild instead
Its working fine only if DashboardModule is explicitly imported in AppRoutingModule
Below is a comprehensive example of using
lazy-loading,sharing-components between modules, But first lets look at what went wrong!The
forRootshould be used for the root level module ( hereAppModule) for all the lazy loaded routes we needforChild, in your example for theapp.routing.tsyou are usingforChild, so I guess thats where it went wrong!You only need to export the essential components that need to be shared to other components, so there is no need to export
RouterModule, this can be done only if you are defining a separate file for routesmodule1.routing.tsthere is no need to export it!Now lets focus on sharing components/directives.
If you are going to use something that is shared by all the modules, its better you create a module called
SharedModulethis will contain the common items for all modules, egCommonModuleis definetely required by all the modules, you can also share components. The important part is that, the component/directive needs to be in the declarations array of only one module and should be present in the exports array also for other modules to use it!If you are sharing components/directives between only few modules, you can declare the component only on the original module but you need to add it to the exports array for it to be accessible by other modules, in the below example provided I am exporting
module1Componentfrommodule1and using it in thedashboardModuleapp routing ts
module 1 routing ts
module 1 ts
dashboard module using component of module 1
shared module, stores components/etc which is shared by all!
Stackblitz Demo->cd test->npm i->npm run start