I have an angular app with multiple Modules, which each define some routes.
App (base module) uses this AppRoutingModule:
const routes: Routes = [
{path: 'user', loadChildren: 'app/user/user.module#UserModule'},
{path: 'exchange', loadChildren: 'app/exchange/exchange.module#ExchangeModule'},
{path: 'home', component: HomeComponent},
{path: '', redirectTo: '/home', pathMatch: 'full'},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
User Module Router:
@NgModule({
imports: [RouterModule.forChild(
[{
path: '', component: UserComponent,
children: [
{path: '', component: UserProfileComponent, canActivate: [AuthGuardService]},
{path: 'login', component: UserLoginComponent},
{path: 'register', component: UserRegisterComponent},
{path: 'confirm/:confirmcode', component: UserConfirmMailComponent},
{path: 'logout', component: UserLogoutComponent}
]
}]
)],
exports: [RouterModule]
})
export class UserRoutingModule {
}
Exchange Module Router:
@NgModule({
imports: [RouterModule.forChild(
[{
path: '', component: ExchangeComponent,
children: [
{path: '', component: ExchangeListComponent},
{path: ':exchange-name', component: ExchangeDetailComponent},
{path: ':exchange-name/:baseCode', component: ExchangeSelectionComponent},
{path: ':exchange-name/:baseCode/:targetCode', component: ExchangePairComponent},
]
}]
)],
exports: [RouterModule]
})
export class ExchangeRoutingModule {
}
I can now use the route http://localhost:4200/exchange/Sample/Base for accessing ExchangeSelectionComponent component as expected.
But when I navigate to http://localhost:4200/user/confirm/ and accidentally missed to add the required parameter confirmcode, instead of an Invalid-Route-Error I get matched to the same ExchangeSelectionComponent-route as above.
Also when I try to open http://localhost:4200/thispagedoesnotexist I get to the ExchangeDetailComponent with exchange-name = thispagedoesnotexist.
It seems that everything below /exchange/ also gets matched to the base root /.
Why is that and how can I avoid that behaviour?
I thought that every route defined in a route inside routeChildren only gets lazy loaded, when the corresponding parent-route is accessed and until then is not even touched.
I use Angular 5.2. Thanks in advance for any input.
{path: 'exchange', loadChildren:'app/exchange/exchange.module#can you please write it correctly so we would know what's there. - shehata