Router - Routes Config transform hook
See original GitHub issueπ feature request
Relevant Package
This feature request is for @angular/router
Description
Ability to transform/process routes when they are being registered (in order to be filtered/manipulated or so)
Describe the solution youβd like
@NgModule({
imports: [RouterModule.forChild(ROUTES)],
providers: [
{ provide: RoutesConfigHandler, useClass: AppRoutesConfigTransformer } // register in root
],
exports: [RouterModule]
})
export class AppShellRoutingModule {
}
class AppRoutesConfigTransformer extends RoutesConfigHandler {
constructor(
appInfo: AppInfoService, // custom service
)
process(routes: Routes): Routes { // if it can take `Routes | Promise<Routes> | Observable<Routes>` as return even better but probably complicates things
// perform custom logic on routes - validate data, set some data, filter or so...
return routes;
}
}
Describe alternatives youβve considered
Tried to handle the more angular DI way, however with the router I had no luck.
I tried even custom function for RouterModule.forChild
(as how it is internally) but still nothing works.
@NgModule({
imports: [RouterModule.forChild(ROUTES)],
providers: [
{ provide: ROUTES, useFactory: validateProcessRoutes, deps: [AppInfoService] },
],
exports: [RouterModule]
})
export class AppShellRoutingModule {
}
function validateProcessRoutes(routes: Routes) {
// perform custom logic on routes - validate data, set some data, filter or so...
return routes;
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:13 (8 by maintainers)
Top Results From Across the Web
How React Hooks can replace React Router - LogRocket Blog
Looking for an alternative form of routing in your React projects? Read more to find out how React Hooks can replace React Router....
Read more >Chapter: Configuring Static Routing - Cisco
This chapter describes how to configure static routing on the switch. This chapter includes the following sections: β’ Information About Static Routing.
Read more >Specifying a Route's Model - Ember Guides
In the model hook for routes with dynamic segments, it's your job to turn the ID (something like 47 or post-slug ) into...
Read more >How To Handle Routing in React Apps with React Router
In this tutorial, you'll install and configure React Router, build a set of routes, and connect to them using the <Link> component.
Read more >Common Routing Tasks - Angular
To set up a redirect, configure a route with the path you want to redirect from, the component you want to redirect to,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oki so one of the examples i mentioned is to iterate through routes and add validation. Again the only problem we have is we are not able to use DI, so the requested feature is to enable DI pretty much, in this case a simple logger.
Validate the Routes has certain data e.g. RouteId in this case
This is how it currently is, and
it works(update: doesnt work with AOT) , however in this example we cannot use our own logger (commented below)Now in order to be able to use the logger we would need DI, and this is how we generally create it
So again, the problem is that we are currently limited to only write code which doesnβt use any services.
Another issue is you need to apply this manually for each individual usage, like this:
While with the other, you register the service once in root and thats it.
Thanks for taking the time to write this up, I appreciate it! I think understanding the use-case helps in finding solutions, be it existing ones or to think about a possible new feature. Iβll follow up with my thoughts on this below, but letβs see what the Angular team thinks about it. π
Since this is about preventing misconfiguration during development, wouldnβt you rather want this to be a compile-time error rather than a runtime one, though? This could probably be covered in a test that scans the source for routing modules and then performs the checks (although admittedly I donβt know of specific tooling that makes writing such tests straight-forward).
As for a runtime version you could have a service which injects Router and then parses Router#config this way to log the errors. This wouldnβt disable those routes, though this could then still be achieved by keeping that validation result and accessing it from a guard.