I have a route defined as /abc/:id/xyz
Where abc/:id resolves to a ComponentA and /xyz is a child component displayed in a router-outlet (ComponentB)
When navigating to /abc/:id/xyz, when I do this.route.params.subscribe(...) (where route is an ActivatedRoute) in ComponentA i see the :id. When doing the same thing in ComponentB I do not see :id. I am guessing ActivatedRoute is using url segments.
Is there anyway to get all the parameters in the route?
To access the route parameters, we use route. snapshot , which is the ActivatedRouteSnapshot that contains information about the active route at that particular moment in time. The URL that matches the route provides the productId . Angular uses the productId to display the details for each unique product.
RouterState and ActivatedRoute are similar to their snapshot counterparts except that they expose all the values as observables, which are great for dealing with values changing over time. Any component instantiated by the router can inject its ActivatedRoute.
The first way is through the route snapshot. The route snapshot provides the initial value of the route parameter map (called the paramMap ). You can access the parameters directly without subscribing or adding observable operators. The paramMap provides methods to handle parameter access like get , getAll , and has .
To follow up on Mark's answer this seems to be the method that works in the final version of Angular 2:
constructor(route: ActivatedRoute) {
let id = route.pathFromRoot[route.pathFromRoot.length - 1].snapshot.params["id"];
}
Update: Direct access to parent route snapshot from here:
constructor(route: ActivatedRoute) {
let id = route.snapshot.parent.params['id'];
}
See also Angular 2: getting RouteParams from parent component for other options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With