Linking to Other Pages
A plain href makes the browser reload the whole app. routerLink lets the router change the page instead, routerLinkActive marks the link of the page you're on, and Router's navigate goes to another page from code, such as after saving.
Apprentice · Angular · Updated
After this lesson you can
- Link between pages with routerLink, and mark the current page's link with routerLinkActive
- Go to another page from code with Router's navigate
Key takeaways
- Link inside the app with
routerLink, nothref: anhrefstarts the whole app again, androuterLinkonly swaps the page. RouterLinkmust be in the component'simports, or the link does nothing.routerLinkActive="active"marks the link of the page you're on. The Home link also needs[routerLinkActiveOptions]="{ exact: true }".- When the code decides where to go, such as after saving, inject
Routerand callnavigate(['/categories']).
Visitors don't type addresses: they click links. But an ordinary link does the wrong thing in an Angular app.
Why not href?
<a href="/categories"> tells the browser to load a new page. It asks the server for /categories, gets index.html back, and starts the whole app again, from nothing. It works, but the page flashes, it's slower, and anything the app was holding, such as a cart in a service, is lost.
routerLink tells the router instead. The router changes the address and swaps the component in <router-outlet />, and the app keeps running.
routerLink
Make a header component for the links, ng generate component site-header:
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive } from '@angular/router';
@Component({
imports: [RouterLink, RouterLinkActive],
selector: 'app-site-header',
template: `
<nav class="nav nav-pills">
<a class="nav-link" routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Home</a>
<a class="nav-link" routerLink="/categories" routerLinkActive="active">Categories</a>
<a class="nav-link" routerLink="/products" routerLinkActive="active">Products</a>
</nav>
`,
})
export class SiteHeader {}
routerLink="/categories"goes to that address. Here the address does start with a slash: it's the whole address, from the start, like anhref.RouterLinkmust be in the component'simports. Without it,routerLinkis just an attribute that does nothing, and the link isn't a link at all.- Angular still writes an
hrefon the<a>, so Ctrl+click opens the page in a new tab, as people expect.
Put <app-site-header /> in app.html, in place of the Northwind heading, and add SiteHeader to App's imports.
routerLinkActive
routerLinkActive="active" adds the class active to the link while its page is showing, and takes it away when you leave. Bootstrap's nav-pills show the active link highlighted, so visitors can see where they are.
The Home link needs [routerLinkActiveOptions]="{ exact: true }". Without it, a link is active for its address and every address that starts with it, and every address starts with /, so Home would always look active. exact: true makes it active only at exactly /.
Going to a page from code
Sometimes the app should change page by itself: after saving a form, say, it goes back to the list. Inject the Router, and call navigate:
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
@Component({
imports: [],
selector: 'app-category-form',
template: `<button class="btn btn-primary" (click)="save()">Save</button>`,
})
export class CategoryForm {
private readonly router = inject(Router);
save() {
// ...save the category with the service, then:
this.router.navigate(['/categories']);
}
}
navigate takes the address as a list of parts: ['/categories'] here. The next topic shows why it's a list. navigateByUrl('/categories') does the same with one string.
Use routerLink when the visitor chooses where to go, and navigate when the code decides.
Do it: decide which links are highlighted
routerLinkActive highlights a link while its page is showing. Write its rule yourself, in plain JavaScript, and see why the Home link needs exact: true.
The function gets three values:
link: the link's address, as written inrouterLink('/categories').current: the address the browser is showing now ('/categories/3').exact:truewhen the link has{ exact: true }, otherwisefalse.
For example, the Home link, '/', while the browser shows '/categories', without exact:
| Question | Answer |
|---|---|
Is '/categories' exactly '/'? |
No |
Does '/categories' start with '/'? |
Yes |
So isActive('/', '/categories', false) returns true: without exact, Home looks active on every page. With exact, isActive('/', '/categories', true) returns false.
Your job: write the function isActive(link, current, exact), which returns true or false.
- When
exactistrue, the link is active only whencurrentis exactly the same aslink. - When
exactisfalse, the link is active whencurrentis exactlylink, or starts withlink.
Practiced live on the lesson page: write the JavaScript right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Inside the app, link with routerLink, never href: the router changes the page without starting the app again. routerLinkActive shows visitors where they are, and Router's navigate moves them on from code.