I want to use the RouterModule.register(arrayOfRoutes), but I need to build the arrayOfRoutes with a variable from the ConfigService.
What is the proper way to do that ?
Thanks for the help ! ;)
// current state, appRoutes is not customizable.
// app.routes.ts
export const appRoutes: Routes = [
{ path: RouteEnum.EndpointA, module: EndpointAModule },
{ path: RouteEnum.EndpointB, module: EndpointBModule },
];
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({ load: loadConfiguration(), cache: true, isGlobal: true }),
RouterModule.register(appRoutes)
],
})
export class AppModule {}
I’d like to use some kind of service like this :
// routes.service.ts
@Injectable()
export class RoutesService {
#routes: Routes = [
{ path: RouteEnum.EndpointA, module: EndpointAModule },
{ path: RouteEnum.EndpointB, module: EndpointBModule },
];
constructor(private readonly configService: ConfigService) {
if (this.configService.get('endpoint-c')?.enabled === true) {
this.#routes.push({ path: RouteEnum.EndpointC, module: EndpointCModule });
}
}
getRoutes(): RouteTree[] {
return this.#routes;
}
}