Explain ActivatedRoute.
Skip to solutionKEEP THE
mediumFrontend
How do you read route parameters?
415 views
01
Understand the problem
routingparams
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Inject ActivatedRoute and read paramMap/queryParamMap as observables (or snapshots). Observables are preferred when the same component is reused across param changes, so you react to navigation without re-instantiating.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Reactive param reading + refetch
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { switchMap } from 'rxjs';
@Component({ selector: 'app-user', standalone: true, template: '{{ id }}' })
export class UserComponent {
private route = inject(ActivatedRoute);
id = '';
constructor() {
// one-time:
this.id = this.route.snapshot.paramMap.get('id') ?? '';
// reactive (param changes in place) — refetch on each id:
this.route.paramMap
.pipe(switchMap(p => fetch('/users/' + p.get('id')).then(r => r.json())))
.subscribe(user => console.log(user));
}
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 61 of 121 decoded in the Angular track. One more won't hurt.