How do you declare a route parameter?
A route parameter is declared with a colon : in the Routes configuration.
Example:
ts
const routes: Routes = [
{ path: 'user/:id', component: UserComponent }
];Here :id is the route parameter.
For a path like /user/10, Angular will load UserComponent, and the value 10 becomes the id parameter.
To get this parameter in the component, use ActivatedRoute:
ts
constructor(private route: ActivatedRoute) {}
ngOnInit() {
const id = this.route.snapshot.paramMap.get('id');
}Summary:
- In a route, a parameter is declared with
path: 'route/:param'. - In the URL, it is substituted dynamically.
- In the component, it is read through
ActivatedRoute.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.