I have an app that is divided into a authenticated section (InternalRootComponent) and an anonymous section (ExternalRootComponent).
Everything works fine when I navigate to the routes explicitly, but when I go to the root (/), I don't get redirected. Also, the AccountsComponent is loaded for some reason.
app-routing.module.ts:
export const routes: Routes = [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
component: ExternalRootComponent,
children: [
{
path: '',
loadChildren: './login/login.module#LoginModule'
}
]
},
{
path: 'membership',
component: ExternalRootComponent,
children: [
{
path: '',
loadChildren: './membership/membership.module#MembershipModule'
}
]
},
{
path: 'app',
component: InternalRootComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
canActivateChild: [AuthGuard],
children: [
{
path: '',
redirectTo: './dashboard',
pathMatch: 'full'
},
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
{
path: 'accounts',
loadChildren: './accounts/accounts.module#AccountsModule'
},
{
path: 'users',
loadChildren: './users/users.module#UsersModule'
},
{
path: 'services',
loadChildren: './services/services.module#ServicesModule'
},
{
path: 'support',
loadChildren: './support/support.module#SupportModule'
}
]
}
]
},
{
path: '**',
component: NotFoundComponent
}
];
accounts-routing.module.ts:
const routes: Routes = [
{
path: '',
component: AccountInfoComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AccountsRoutingModule {}
What I don't understand is why the first redirect doesn't work - I would expect / to redirect to /login. Instead, it seems as if the empty route in accounts-routing.module.ts is being invoked.
My guess is that AccountModule is imported into the root module.
This is a generic setup that should work. Sorry I did not use all of your code because I thought it would be more clear with a minimum yet complete example. I put comments of potential problems that would lead to the behavior you are observing. I can't be entirely sure this will solve your exact problem without more info but it is at the very least similar and should be helpful to somebody.
take the following setup that uses module lazy loading:
NOTE - lazy loading can lead to unexpected behavior because of the the router module importing of child routes, especially if you have services bundled into your feature modules which necessitates root level imports (probably best to separate the services into their own modules though). Comments below should explain what I mean by this.
The lesson is to only import lazy modules with routes once. (Not doing so means the module can no longer be lazy and defeats the purpose of the lazy loading altogether) if you have services bundled in with them that need to be in the root separate those into a different service module for the root
app.module.ts
app.component.ts
app-routing.module.ts
NOTE - lazy loaded modules import RouterModule.forChild(routes) which can lead to unexpected behavior if not careful
male-child.module.ts
female-child.module.ts
john-child.component.ts
sally-child.component.ts