📖 Scenario: You are building a simple Angular app with two pages: Home and About. You want users to click links to move between these pages without reloading the whole website.
🎯 Goal: Create navigation links using Angular's RouterLink directive to switch between Home and About pages smoothly.
📋 What You'll Learn
Create a basic Angular component with a navigation menu
Add a variable to hold the route paths
Use RouterLink directive to link to Home and About routes
Add the final router-outlet tag to display routed components
💡 Why This Matters
🌍 Real World
Most Angular apps use RouterLink to let users move between pages smoothly without full page reloads, improving user experience.
💼 Career
Understanding RouterLink is essential for Angular developers to build multi-page apps with clean navigation and good performance.
Progress0 / 4 steps
1
Create the routes object
Create a constant called routes that is an array with two objects: one with path set to '' and component set to HomeComponent, and another with path set to 'about' and component set to AboutComponent.
Angular
Hint
Think of routes as a map telling Angular which component to show for each URL path.
2
Add a variable for navigation links
Create a variable called navLinks that is an array with two objects: one with label set to 'Home' and path set to '', and another with label set to 'About' and path set to 'about'.
Angular
Hint
This variable will help you create clickable links in the template.
3
Use RouterLink in the template
In the component template, write a <nav> element containing an <a> tag for each item in navLinks using *ngFor="let link of navLinks". Add the [routerLink] directive binding to link.path and display link.label inside the <a> tag.
Angular
Hint
Use Angular's *ngFor to loop over navLinks and [routerLink] to set the link path.
4
Add the router-outlet tag
Add the <router-outlet></router-outlet> tag below the <nav> element in the template to display the routed components.
Angular
Hint
This tag tells Angular where to show the page content for the current route.
Practice
(1/5)
1. What is the main purpose of RouterLink in Angular?
easy
A. To fetch data from a server
B. To style buttons with CSS classes
C. To create clickable links that navigate within the app without reloading the page
D. To define routes in the app module
Solution
Step 1: Understand RouterLink's role
RouterLink is used to create links that change the URL and navigate inside the Angular app smoothly.
Step 2: Differentiate from other features
It does not style buttons, fetch data, or define routes; those are separate tasks.
Final Answer:
To create clickable links that navigate within the app without reloading the page -> Option C
Quick Check:
RouterLink = navigation links [OK]
Hint: RouterLink changes URL inside app without page reload [OK]
Common Mistakes:
Confusing RouterLink with CSS styling
Thinking RouterLink fetches data
Mixing RouterLink with route definitions
2. Which of the following is the correct syntax to create a static link to '/home' using RouterLink in a template?
and userId = 42, what URL will the link navigate to when clicked?
medium
A. /profile?userId=42
B. /profile/42
C. /profile/userId
D. /profile
Solution
Step 1: Understand array syntax in RouterLink
Using [routerLink]="['/profile', userId]" creates a URL by joining segments, so it appends userId as a path segment.
Step 2: Substitute userId value
With userId = 42, the URL becomes /profile/42.
Final Answer:
/profile/42 -> Option B
Quick Check:
Array routerLink joins segments with slashes [OK]
Hint: Array syntax adds path segments separated by slashes [OK]
Common Mistakes:
Thinking it adds query parameters instead of path segments
Using string interpolation inside array incorrectly
Ignoring dynamic value substitution
4. Identify the error in this RouterLink usage:
<a [routerLink]="'/dashboard'">Dashboard</a>
medium
A. The binding brackets should be removed for static paths
B. RouterLink cannot be used on <a> tags
C. The path string should not be in quotes inside the binding brackets
D. Missing parentheses around the path
Solution
Step 1: Analyze binding syntax
Using square brackets means Angular expects an expression. Here, the expression is a string literal with quotes, which is valid but unnecessary for static paths.
Step 2: Correct usage for static paths
For static paths, remove square brackets and quotes: routerLink="/dashboard".
Final Answer:
The binding brackets should be removed for static paths -> Option A
Quick Check:
Static path = no brackets [OK]
Hint: Remove brackets for static string paths [OK]
Common Mistakes:
Using quotes inside binding brackets unnecessarily
Thinking RouterLink can't be on anchor tags
Adding parentheses around path
5. You want to create a navigation link that goes to a user detail page with a dynamic user ID, but only if the ID is valid (non-null). Which is the best way to use RouterLink in the template to handle this condition?
<a [routerLink]="userId ? ['/user', userId] : ['/user']">User Details</a> uses a ternary operator inside the binding to choose the path based on userId validity, which is clean and safe.
Step 2: Evaluate other options
<a *ngIf="userId" [routerLink]="['/user', userId]">User Details</a> hides the link if userId is falsy, which may be okay but removes the link entirely. <a [routerLink]="['/user', userId || 'default']">User Details</a> uses a fallback string which might not be desired. <a [routerLink]="/user/{{userId}}">User Details</a> uses interpolation inside the binding expression, which is invalid template syntax.