The route configuration of a lazy-loaded module is structured differently because lazy-loaded modules have their own separate route configurations. In my case, I have routes fetched from the database and need to apply them to the router.config when the user successfully logs in.
the core module is lazyloaded in the app module on path 'core'.
app-routing.modules.ts
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'core', loadChildren: () => import('@modules/pu-core/pu-core.module').then(m => m.PuCoreModule) }
];
Core module
const routes: Routes = [
{ path: 'layout', component: LayoutComponent, childre:[] },
];
on the success of login, the auth service has an observable apiRoutes$ that holds the routes that needs to be applied to the layout path children.
Using of this.router.config to dynamically add the children doesn't work since the config is different.
The below implementation doesn't work:
constructor(public router: Router, private activatedRoute: ActivatedRoute, @SkipSelf() private auth: AuthService) {
this.auth.routes$.subscribe(
(routesData: any) => {
this.router.config[2].children = routesData;
}
)
}