Router.navigate()
Programmatically navigates to a route from component code.
Syntax
router.navigate(commands: any[], extras?): Promise<boolean> Parameters
| Name | Type | Required | Description |
|---|---|---|---|
commands | any[] | No | Array of route segments and params. |
extras | NavigationExtras | No | Options like queryParams, relativeTo, replaceUrl. |
Returns
Promise<boolean> — Resolves true if navigation succeeded.
Examples
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
@Component({ selector: 'app-x', standalone: true, template: '' })
export class XComponent {
private router = inject(Router);
goToUser(id: number) {
this.router.navigate(['/users', id], {
queryParams: { ref: 'list' },
});
}
}
Notes
Use navigate() with a commands array for structured paths, or navigateByUrl()
with an absolute URL string. Provide relativeTo with an ActivatedRoute for
relative navigation. The returned promise reports success/failure (e.g.
blocked by a guard).