I'm trying to develop SPA using angular 9, I almost try to lazy load every component and all of its children. My problem arose when I tried to have router-outlet inside one of the lazy-loaded components and I want this router-outlet to be used to load children components (which is lazy-loaded also). when I do so, I always get all the nested lazy-loaded components loaded in the main router-outlet in the app.component.html instead of the router-outlet in the nested lazy-loaded component
app.component template:
<app-navbar></app-navbar>
<router-outlet></router-outlet>
app-routing.module:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: 'articles', loadChildren: () => import('./articles-viewer/articles-viewer.module').then(m => m.ArticlesViewerModule) },
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) },
{ path: '**', redirectTo: ''}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module:
@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent,
NavbarComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
BrowserAnimationsModule,
AppAngularMaterial
],
providers: [
AppHttpInterceptors
],
bootstrap: [AppComponent]
})
export class AppModule { }
navbar.component template:
<a routerLink="">Home</a>
<a routerLink="/articles">Articles</a>
<a routerLink="/dashboard">Dashboard</a>
dashboard template:
<a routerLink="statistics">Statistics</a>
<router-outlet></router-outlet>
dashboard.module :
@NgModule({
declarations: [DashboardComponent],
imports: [
CommonModule,
DashboardRoutingModule,
AppAngularMaterial
]
})
export class DashboardModule { }
dashboard-routing.module:
const routes: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'statistics', loadChildren: () => import('./statistics/statistics.module').then(m => m.StatisticsModule) }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class DashboardRoutingModule { }
In a nutshell, the statistics component (a lazy-loaded component) is loaded in the router-outlet declared in the app.component template rather than in the router-outlet in the dashboard template and the dashboard component is unloaded
I tried to find the solution in angular.io but all that I got is about managing multiple outlets in the same component. Also, this is what is I got when googling this problem.
is there any solution for this?
