RouterModule helps your Angular app switch between different pages or views without reloading the whole page.
RouterModule configuration in Angular
import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: '**', component: NotFoundComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
The routes array defines URL paths and which component to show.
RouterModule.forRoot(routes) sets up the router with your routes.
const routes: Routes = [ { path: 'dashboard', component: DashboardComponent } ];
const routes: Routes = [ { path: '', redirectTo: 'dashboard', pathMatch: 'full' } ];
const routes: Routes = [ { path: '**', component: PageNotFoundComponent } ];
This example sets up a simple Angular app with three routes: home, about, and a fallback for unknown pages. Navigation links let you switch pages without reloading. The router-outlet shows the matched component.
import { Component } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { bootstrapApplication } from '@angular/platform-browser'; @Component({ selector: 'app-home', standalone: true, template: `<h2>Home Page</h2><p>Welcome to the home page!</p>` }) class HomeComponent {} @Component({ selector: 'app-about', standalone: true, template: `<h2>About Page</h2><p>Learn more about us here.</p>` }) class AboutComponent {} @Component({ selector: 'app-not-found', standalone: true, template: `<h2>Page Not Found</h2><p>Sorry, this page does not exist.</p>` }) class NotFoundComponent {} const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: '**', component: NotFoundComponent } ]; @Component({ selector: 'app-root', standalone: true, imports: [RouterModule, HomeComponent, AboutComponent, NotFoundComponent], template: ` <nav> <a routerLink="/home" aria-label="Go to Home page">Home</a> | <a routerLink="/about" aria-label="Go to About page">About</a> </nav> <main> <router-outlet></router-outlet> </main> ` }) class AppComponent {} bootstrapApplication(AppComponent, { providers: [ RouterModule.forRoot(routes) ] });
Always include a wildcard ** route to catch unknown URLs and show a friendly message.
Use pathMatch: 'full' when redirecting empty paths to avoid partial matches.
Remember to add RouterModule to your component imports and exports for routing to work.
RouterModule lets Angular apps switch views based on URL paths.
Define routes as an array of path-component pairs.
Use RouterModule.forRoot(routes) to set up routing in your app.
Practice
RouterModule.forRoot(routes) in an Angular app?Solution
Step 1: Understand RouterModule role
RouterModule is used to manage navigation and views based on URL paths in Angular.Step 2: Role of forRoot method
The forRoot method sets up the routes for the whole app, telling Angular which component to show for each path.Final Answer:
To configure the app's routes and enable navigation based on URL paths -> Option BQuick Check:
RouterModule.forRoot sets routes [OK]
- Confusing forRoot with creating components
- Thinking RouterModule styles components
- Assuming it fetches data
Solution
Step 1: Check Angular module imports syntax
Angular modules expect an array for the imports property.Step 2: Correct use of RouterModule.forRoot
RouterModule.forRoot(routes) returns a module configured with routes and must be inside an array.Final Answer:
imports: [RouterModule.forRoot(routes)] -> Option CQuick Check:
Imports must be an array with forRoot call [OK]
- Omitting the array brackets in imports
- Using RouterModule.import which doesn't exist
- Calling RouterModule as a function directly
const routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];What component will Angular display when the URL path is
/about?Solution
Step 1: Match URL path to route
The URL path '/about' matches the route with path 'about'.Step 2: Identify component for matched route
The route with path 'about' uses AboutComponent.Final Answer:
AboutComponent -> Option AQuick Check:
Path '/about' shows AboutComponent [OK]
- Assuming empty path '' matches '/about'
- Thinking multiple components show at once
- Expecting error for valid path
const routes = [
{ path: 'home', component: HomeComponent },
{ path: 'profile' component: ProfileComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}Solution
Step 1: Check routes array syntax
Between 'path: 'profile'' and 'component: ProfileComponent' a comma is missing, causing syntax error.Step 2: Verify RouterModule usage and exports
Using RouterModule.forRoot(routes) and exporting RouterModule is correct here.Final Answer:
Missing comma between 'profile' path and component property -> Option DQuick Check:
Comma missing in object properties causes error [OK]
- Confusing forRoot and forChild usage
- Placing routes array incorrectly
- Removing exports of RouterModule
Solution
Step 1: Understand redirectTo usage
To redirect from '' path, use redirectTo with full path and specify pathMatch as 'full' to match exact empty path.Step 2: Check options for correct syntax
{ path: '', redirectTo: '/home', pathMatch: 'full' } correctly uses redirectTo with '/home' and pathMatch: 'full'. { path: '', redirectTo: 'home' } misses pathMatch, C wrongly combines component and redirectTo, D uses 'prefix' which causes partial matching issues.Final Answer:
{ path: '', redirectTo: '/home', pathMatch: 'full' } -> Option AQuick Check:
Redirect empty path with pathMatch 'full' [OK]
- Omitting pathMatch property
- Combining component with redirectTo
- Using pathMatch: 'prefix' for empty path redirect
Ready to prove it?
Pass the test and this pattern moves unknown
