Routing helps single-page apps show different pages without reloading the whole site. It makes the app feel fast and smooth like a real website.
Why routing is needed for SPAs in Angular
Introduction
Syntax
Angular
const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: 'home', pathMatch: 'full' } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
Routes define which component shows for each URL path.
RouterModule.forRoot sets up routing for the whole app.
Examples
Angular
const routes: Routes = [ { path: 'dashboard', component: DashboardComponent }, { path: 'profile', component: ProfileComponent } ];
Angular
const routes: Routes = [ { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, { path: '**', component: PageNotFoundComponent } ];
Sample Program
This example shows routing setup with two pages: Home and About. The app starts at Home. Clicking links changes the URL and shows the right page without reloading.
Angular
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component'; import { AboutComponent } from './about.component'; const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: 'home', pathMatch: 'full' } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } // HomeComponent import { Component } from '@angular/core'; @Component({ selector: 'app-home', template: `<h1>Home Page</h1><p>Welcome to the home page!</p>` }) export class HomeComponent { } // AboutComponent import { Component } from '@angular/core'; @Component({ selector: 'app-about', template: `<h1>About Page</h1><p>Learn more about us here.</p>` }) export class AboutComponent { }
Important Notes
Routing keeps your app fast by not reloading the whole page.
Use routerLink in templates to create clickable links that change views.
Always define a default route to avoid blank pages.
Summary
Routing lets SPAs show different pages without full reloads.
It improves user experience by making navigation fast and smooth.
Angular routing uses routes to connect URLs to components.
Practice
1. Why do Single Page Applications (SPAs) like Angular apps need routing?
easy
Solution
Step 1: Understand SPA behavior
SPAs load a single HTML page and dynamically update content without full reloads.Step 2: Role of routing in SPAs
Routing allows changing views or components based on URL without reloading the entire page, making navigation smooth.Final Answer:
To change views without reloading the whole page -> Option BQuick Check:
Routing enables smooth view changes = D [OK]
Hint: Routing lets SPAs switch views without full page reloads [OK]
Common Mistakes:
- Thinking SPAs reload the whole page on navigation
- Believing routing slows down the app
- Confusing routing with disabling navigation
2. Which of the following is the correct way to define a route in Angular?
easy
Solution
Step 1: Recall Angular route syntax
Angular routes use objects withpathandcomponentkeys.Step 2: Match correct keys
Only{ path: 'home', component: HomeComponent }usespathandcomponentcorrectly.Final Answer:
{ path: 'home', component: HomeComponent } -> Option DQuick Check:
Angular routes use path and component keys = A [OK]
Hint: Angular routes use 'path' and 'component' keys [OK]
Common Mistakes:
- Using wrong keys like url or route instead of path
- Confusing component with element or view
- Mixing route object structure with HTML attributes
3. Given this Angular route configuration:
What component will display when the URL is
const routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];What component will display when the URL is
/about?medium
Solution
Step 1: Match URL to route path
The URL/aboutmatches the route withpath: 'about'.Step 2: Identify component for matched route
The route withpath: 'about'usesAboutComponent.Final Answer:
AboutComponent -> Option CQuick Check:
URL '/about' loads AboutComponent = C [OK]
Hint: URL path matches route path to load component [OK]
Common Mistakes:
- Assuming empty path matches '/about'
- Thinking HomeComponent loads for all URLs
- Expecting error or blank page without reason
4. What is wrong with this Angular routing code?
The app does not navigate to the About page when clicking the link
const routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}The app does not navigate to the About page when clicking the link
<a routerLink='/about'>About</a>.medium
Solution
Step 1: Check routing module setup
The routing module is defined correctly but must be imported in the main app module to work.Step 2: Identify missing import
IfAppRoutingModuleis not imported inAppModule, routing won't activate, causing navigation failure.Final Answer:
RouterModule is not imported in the main app module -> Option AQuick Check:
Routing module must be imported in app module = B [OK]
Hint: Always import routing module in main app module [OK]
Common Mistakes:
- Thinking routerLink attribute is case-sensitive
- Assuming default route is required for all navigation
- Blaming component names without error evidence
5. You want your Angular SPA to show a 'Not Found' page when users enter an unknown URL. How should you add this behavior using routing?
hard
Solution
Step 1: Understand wildcard route usage
Angular usespath: '**'as a wildcard to catch all unmatched URLs.Step 2: Assign NotFoundComponent to wildcard route
Defining{ path: '**', component: NotFoundComponent }shows the Not Found page for unknown URLs.Final Answer:
Add a route with path: '**' and a NotFoundComponent -> Option AQuick Check:
Wildcard '**' route handles unknown URLs = A [OK]
Hint: Use '**' path to catch unknown URLs for Not Found page [OK]
Common Mistakes:
- Using empty path '' for Not Found page
- Manually redirecting unknown URLs instead of wildcard
- Expecting Angular to handle unknown URLs without setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
