I have my routes configured in @NgModule
. I also have a service that determines which parts of the application to display based on specific conditions. I need to call this service and adjust the routes according to its output.
Issue: The route configuration is defined within an annotation, and I am unsure how to access the service in this setup.
To clarify, here is the current routing configuration I am looking to improve:
Current routing setup:
const appRoutes: Routes = [
{
path: '',
redirectTo: 'first-route',
pathMatch: 'full'
},
{
path: 'first-route',
component: FirstComponent,
pathMatch: 'full'
},
{
path: 'second-route',
component: SecondComponent,
pathMatch: 'full'
},
...
];
@NgModule({
imports: [RouterModule.forChild(appRoutes)],
exports: [RouterModule]
})
export class MyRoutingModule {
}
The service responsible for changing the route setup:
@Injectable()
export class MyService {
getAccessibleRoutes(): Observable<string[]> {...}
}
Query: How can I invoke the service and modify the routes?
Note: I have also explored discussions on "Dynamically adding routes in Angular" and "How we can add new routes dynamically into RouterModule(@NgModule imports)", but have not found a clear solution there.