How do you create a clickable link to navigate between pages using Angular's router?
Short answer
Instead of a regular HTML anchor tag, which would cause a full page reload, Angular provides the routerLink directive that changes the current page smoothly using client side routing, while still looking and working like a normal link to the user.
Advertisement
AModel answer
Instead of a regular HTML anchor tag, which would cause a full page reload, Angular provides the routerLink directive that changes the current page smoothly using client side routing, while still looking and working like a normal link to the user.
<nav>
<a routerLink="/">Home</a>
<a routerLink="/about">About</a>
</nav>Real-world example
A website's navigation menu uses the routerLink directive instead of regular anchor tags, so clicking between pages feels instant and smooth instead of triggering a full page reload every time.
Likely follow-ups
- What happens if you accidentally use a regular anchor tag instead of routerLink for internal navigation?
- How does the routerLinkActive directive help highlight the currently active link?
Related topics