Explain it simply
routerLink & router-outlet, in plain language
Instead of the <a> tag, use the routerLink directive for navigation.
Problem → Why → Solution
The problem — why this concept matters
Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.
- URL or navigation
- router config and guard
- component view
Explain the core concept and Angular data flow of routerLink & router-outlet
Core syntax or pattern
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>
// app-routing.module.ts
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.
Read the code step by step
- URL or navigation
<nav>Instead of the <a> tag, use the routerLink directive for navigation.
- router config and guard
// app-routing.module.tsrouterLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.
- component view
];Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.
- URL or navigation
- router config and guard
- component view
Success
Explain the core concept and Angular data flow of routerLink & router-outlet
Failure
Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.
Instead of the <a> tag, use the routerLink directive for navigation.
<router-outlet> is a placeholder directive that tells the Angular router where to display the activated component.
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>
// app-routing.module.ts
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];(Clicking the Home and About links swaps in the matching component's content without refreshing the page)Real-world usage
How to apply it in a real project
Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.
Mini challenge
Build it yourself
Rebuild the routerLink & router-outlet example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>
// app-routing.module.ts
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];Expected: Explain the core concept and Angular data flow of routerLink & router-outlet
Show hint
Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- URL or navigation → router config and guard → component view
- routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.
- Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.
- Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.
Angular — Routing — Angular