I've been attempting to dynamically import routes from a configuration file using the following code snippet:
export function buildRoutes(options: any, router: Router, roles: string[]): Routes {
const lazyRoutes: Routes = Object.keys(options)
.map((key) => {
const entry: RouteConf = options[key];
const auth = entry.authRoute as string[];
let output: Route = {
path: entry.routePath,
redirectTo: 'fallback',
};
if (auth.some(item => roles.includes(item))){
console.log(entry.exposedModule,entry.ngModuleName);
output = {
path: entry.routePath,
loadChildren: () => import(entry.exposedModule).then((m) => m[entry.ngModuleName])
}
}
return output;
});
return [...lazyRoutes,...APP_ROUTES ];
}
however, I'm encountering the following error:
ERROR Error: Uncaught (in promise): Error: Cannot find module '../richieste/richieste.module'
Error: Cannot find module '../richieste/richieste.module'
if I manually set the same data in the loadChildren, like this:
....
loadChildren: () => import( '../richieste/richieste.module').then((m) => m["RichiesteModule"])
....
everything works fine.
Is there a way to dynamically import modules for LazyLoading?
Thanks