skills/dev-skills/angular-developer/references/navigate-to-routes.md
Angular provides both declarative and programmatic ways to navigate between routes.
RouterLink)Use the RouterLink directive on anchor elements.
import {RouterLink, RouterLinkActive} from '@angular/router';
@Component({
imports: [RouterLink, RouterLinkActive],
template: `
<nav>
<a routerLink="/dashboard" routerLinkActive="active-link">Dashboard</a>
<a [routerLink]="['/user', userId]">Profile</a>
</nav>
`,
})
export class Nav {
userId = '123';
}
/ (e.g., /settings)./. Use ../ to go up a level.Router)Inject the Router service to navigate via TypeScript code.
router.navigate()Uses an array of commands.
private router = inject(Router);
private route = inject(ActivatedRoute);
// Standard navigation
this.router.navigate(['/profile']);
// With parameters
this.router.navigate(['/search'], {
queryParams: { q: 'angular' },
fragment: 'results'
});
// Relative navigation
this.router.navigate(['edit'], { relativeTo: this.route });
router.navigateByUrl()Uses a string path. Ideal for absolute navigation or full URLs.
this.router.navigateByUrl('/products/123?view=details');
// Replace current entry in history
this.router.navigateByUrl('/login', {replaceUrl: true});
/user/123).? (e.g., /search?q=query)./products;category=books).