Route parameters and query parameters
Reading :id-style route segments and ?key=value query strings through ActivatedRoute — snapshot versus reactive.
Route parameters and query parameters
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A `:id`-style segment in a route's path (`users/:id`) becomes a route parameter, read through `ActivatedRoute` — not a separate mechanism from query params, just a different part of the URL.
- `.snapshot.paramMap` reads a parameter once, at the moment the component was activated; `.paramMap` as an observable reads it reactively, which matters the instant a component can be reused for a different id without being destroyed and recreated.
- Query parameters (`?sort=name`) are optional, order-independent key-value pairs, read through the same `ActivatedRoute`, via `queryParamMap` instead of `paramMap`.
Two different ways a URL carries extra data
/users/42 and /users?highlight=42 both encode the number 42 into the URL,
but they’re doing structurally different jobs. /users/42 says “this path
segment, 42, is a required, positional part of which resource this route is
about” — a route parameter. /users?highlight=42 says “we’re on the users
route, and here’s some optional extra context attached to it” — a query
parameter. Both are read through the same object, ActivatedRoute, just
through different properties.
Route parameters: :id in the path, read through ActivatedRoute
// routes: { path: 'users/:id', component: UserDetailComponent }
export class UserDetailComponent {
private route = inject(ActivatedRoute);
userId = this.route.snapshot.paramMap.get('id'); // read once
}
:id in the route’s path declares a required, named segment — whatever the
user actually navigates to (/users/42 → id is "42") is available through
ActivatedRoute. .snapshot.paramMap.get('id') reads the value once, at the
moment this code runs — a snapshot, a single point-in-time read, not a live
subscription.
Why the snapshot alone can silently go stale
This is the subtlety worth being careful about. If a user navigates from
/users/1 to /users/2 by clicking a link within an already-rendered
UserDetailComponent, Angular can, by default, reuse the exact same component
instance for the new parameter rather than destroying and recreating it. If your
code only read .snapshot.paramMap once — say, in ngOnInit — that read
happened for id=1 and never happens again, because the component was never
re-initialized for id=2. The displayed data quietly stays frozen on the first
user it loaded.
The fix is reading paramMap as the observable it also is, rather than only its
snapshot:
userId$ = this.route.paramMap.pipe(map((params) => params.get('id')));
Subscribed to (directly, or via the async pipe), this emits a new value every
time the route’s parameters change — including the reused-component case the
snapshot alone misses entirely. The general rule: reach for the snapshot only
when you’re certain the component can’t be reused across a parameter change
(often true for a one-off read in a resolver, covered in the next few lessons);
default to the reactive paramMap observable everywhere else.
Query parameters: queryParamMap, same relationship
sort$ = this.route.queryParamMap.pipe(map((params) => params.get('sort')));
Query parameters work through the identical pattern — queryParamMap instead
of paramMap, with the same snapshot-vs-observable choice applying equally.
Since query parameters are optional by nature, .get('sort') returning null
is an entirely expected, normal case to handle, not an error condition the way
a missing required route parameter usually would be.
Writing query parameters back, without losing the ones already there
Navigation isn’t only about reading parameters — updating them is just as common, for a filter or sort control that should update the URL as the user changes it:
this.router.navigate([], {
relativeTo: this.route,
queryParams: { sort: 'name' },
queryParamsHandling: 'merge',
});
queryParamsHandling: 'merge' matters here more than it looks: without it, this
call would replace the entire query string with just { sort: 'name' },
silently dropping any other query parameters already present in the URL (a
page parameter, say). 'merge' keeps everything already there and only
overwrites the keys you explicitly pass — the difference between a filter
control that cooperates with the rest of the page’s URL state and one that
quietly resets it every time it’s touched.
This lesson’s video covers reading query parameters on a current Angular setup directly; the linked “deeper” video frames the snapshot-vs-reactive choice as its explicit subject, which is worth a dedicated watch given how easy that distinction is to get wrong silently rather than loudly.
Check yourself
A route is configured as `{ path: 'users/:id', component: UserDetailComponent }`. If the user navigates from `/users/1` to `/users/2` by clicking a link within the already-rendered UserDetailComponent, does Angular destroy and recreate the component?
Not necessarily — by default, Angular can reuse the same component instance across a parameter change on the same route, which is exactly why relying on the snapshot alone (read once at activation) can silently show stale data: the component never gets destroyed and recreated for id=2, so a snapshot read at id=1's activation never refreshes. Subscribing to `paramMap` as an observable, instead of reading the snapshot once, is what catches the change.
What's the actual difference between a route parameter and a query parameter, in terms of where each shows up in the URL?
A route parameter is part of the path's structure itself — `:id` in `users/:id` means the segment is required and positional, as in `/users/42`. A query parameter is an optional, order-independent key-value pair appended after a `?`, as in `/users?sort=name&page=2` — it doesn't change which route matches, it's extra data attached to whichever route did match.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.