Bind Route Info to Component Inputs (✨ New Router feature)
Pass router info to routed component inputs
Topics covered in this article:
- How it works today
- How it will work in Angular v16
- How to use it
- How to migrate to the new API
- How to test it
- Caveats
When building applications with Angular, most of the time we use the Router to render different pages for different urls.
And based on the url we also load the data based on its path parameters or query parameters.
In the latest version of Angular v16, we will get a new feature that will simplify the process of retrieving route information in the component and make it way easier.
How it works today
Let's say we have a routes array like this one:
And inside the component we need to read the query params in order to fill a search form.
With an URL like this: http://localhost:4200/search?q=Angular;
As you can see, we need to inject the ActivatedRoute service and then we can access the query params from it. But we can also access the path params and the data, or even the resolved data, as we can see in the following example:
How it will work in Angular v16
In Angular v16 we will get a new feature that will simplify the process of retrieving route information in the component and make it way easier.
We will be able to pass the route information to the component inputs, so we don't need to inject the ActivatedRoute service anymore.
And we can also pass the path params, the data and resolved data to the component inputs:
And of course we can rename the inputs to whatever we want:
How to use it
In order to use this new feature, we need to enable it in the RouterModule:
Or if we are in a standalone application, we can enable it like this:
How to migrate to the new api
If we have a component that is using the ActivatedRoute service, we can migrate it to the new api by doing the following:
- Remove the
ActivatedRouteservice from the component constructor. - Add the
@Input()decorator to the properties that we want to bind to the route information. - Enable the
bindToComponentInputsfeature in theRouterModuleorprovideRouterfunction.
Example with before and after for path params, with url: http://localhost:4200/search/123
How to test it
In order to test the new feature, we can use the RouterTestingHarness and let it handle the navigation for us.
Here is an example of how to test the route info bound to component inputs with the RouterTestingHarness:
It's as simple as that!
Caveats
- Sometimes we want the
idorqueryParamsto be observables, so we can combine them with other observable to get some data.
For example, let's say we have a component that is using the id and queryParams to get some data from the server:
If we want to use the async pipe in order to subscribe to the data, we need to make sure that the id and query are observables instead of strings, otherwise this example below will not work:
In order to make the id and query observables, we can use the BehaviorSubject:
As you can see, we are using the BehaviorSubject to make the id and query observables, and we are using the combineLatest operator to combine them with the switchMap operator to get the data from the server.
Personally, I think that this is a bit too much code for a simple example, so I would recommend to use the ActivatedRoute service instead of the new api in this case.
- Priority of the route information when the route infos have the same name. For example, let's say we have a route with the following configuration:
The new api will bind the route information to the component inputs in the following order:
- Data
- Path params
- Query params
If there's no data, it will use the path params, if there's no path params, it will use the query params If there's no query params, the value input will be undefined!
- We don't know where the input value will come from 😬
In my opinion, for this "issue" what we can do is to rename the Input in imports and use it like this:
Not the best way possible, but we can see that it's not a normal input and that it is connected with the router info.
Play with the feature here: https://stackblitz.com/edit/angular-jb85mb?file=src/main.ts 🎮
Thanks for reading!
If this article was interesting and useful to you, and you want to learn more about Angular, support me by buying me a coffee ☕️ or follow me on X (formerly Twitter) @Enea_Jahollari where I tweet and blog a lot about Angular latest news, signals, videos, podcasts, updates, RFCs, pull requests and so much more. 💎



