Thuta Learning
AdvancedWeb Developmentintermediate

Route Parameters

Relax. We'll talk through this in plain words — no textbook voice.

You can access URL parameters to build dynamic routes.

typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}
You should see
(You can read parameters from the URL and use them inside the component)