NG.V · Angular 22 practice question
Given this standalone component, how does it trigger programmatic navigation to the '/profile' route when the button is clicked?
import {Router} from '@angular/router';
import {Component, inject} from '@angular/core';
@Component({
selector: 'app-dashboard',
template: `<button (click)="goToProfile()">View Profile</button>`,
})
export class AppDashboard {
private router = inject(Router);
goToProfile() {
this.router.navigate(['/profile']);
}
}
Check answer and explanation
Correct answer: It injects Router and passes an array of path segments to router.navigate()
Explanation
Injecting Router with inject(Router) and passing an array of path segments to router.navigate() is the standard way to navigate programmatically in Angular.
Common trap
ActivatedRoute exposes state about the active route rather than providing a navigate() method for triggering new navigations. Angular's Router manages client-side navigation through its own API rather than asking components to mutate window.location.pathname directly.
Source: Angular docs: Navigate to routes — `router.navigate()`
Checked against the cited text on 2026-11-25.