I'm lazy loading modules with Angular, but if I navigate to the URL without providing a route path, e.g. http://localhost:4200
it loads my about.module
when I'd expect the home.module
to be loaded. Eagerly loading the modules works as expected.
Each module has a *-routing.module
:
app-routing.module.ts
/* ... */
const routes: Routes = [
{
path: 'home',
loadChildren: () => import('./modules/home/home.module').then(m => m.HomeModule)
},
{
path: 'about',
loadChildren: () => import('./modules/about/about.module').then(m => m.AboutModule)
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: '**',
redirectTo: 'page-not-found'
}
];
@NgModule({
imports: [
RouterModule.forRoot(
routes
/* ... */
)
],
exports: [RouterModule]
})
/* ... */
home-routing.module.ts
/* ... */
const routes: Routes = [
{
path: '',
component: HomeComponent,
data: {
title: 'Home'
}
}
];
@NgModule({
imports: [
/* ... */
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
/* ... */
about-routing.module.ts
/* ... */
const routes: Routes = [
{
path: '',
component: AboutComponent,
data: {
title: 'About'
}
}
];
@NgModule({
imports: [
/* ... */
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
/* ... */
Other solutions I have tried and are for an older version of Angular don't seem to work:
I'm working with Angular 8+ in this case.
ctrl + c
andng serve
again – terahertz