mediumFrontend

How do you read route parameters?

415 views
01

Understand the problem

Explain ActivatedRoute.

routingparams
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

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.