
- Angular Tutorial
- Angular - Home
- Angular - Overview
- Angular - Features
- Angular - Advantages & Disadvantages
- Angular Basics
- Angular - Environment setup
- Angular - First Application
- Angular - MVC Architecture
- Angular Components
- Angular - Components
- Angular - Component Lifecycle
- Angular - View Encapsulation
- Angular - Component Interaction
- Angular - Component Styles
- Angular - Nested Components
- Angular - Content projection
- Angular - Dynamic components
- Angular - Elements
- Angular Templates
- Angular - Templates
- Angular - Template statements
- Angular - Template Variables
- Angular - SVG as Templates
- Angular Binding
- Angular - Data Binding
- Angular - Interpolation
- Angular - Event Binding
- Angular - Property Binding
- Angular - Attribute Binding
- Angular - Class Binding
- Angular - Style Binding
- Angular - Two-way Binding
- Angular Directives
- Angular - Directives
- Angular - Attribute Directives
- Angular - Structural Directives
- Angular - Custom Directives
- Angular Pipes
- Angular - Pipes
- Angular - Built-in Pipes
- Angular - Custom Pipes
- Angular Forms
- Angular - Forms
- Angular - Template Driven Forms
- Angular - Reactive Forms
- Angular - Form Validation
- Angular - Dynamic Forms
- Angular Dependency Injection
- Angular - Dependency Injection
- Angular - Injectable Service
- Angular Routing
- Angular - Routing
- Angular - Dynamic Routes
- Angular - Wildcard Routes
- Angular - Nested Routes
- Angular - Navigation
- Angular - Routing in SPA
- Angular - Custom Route Matches
- Angular - Router Reference
- Angular HTTP Client programming
- Angular - Services
- Angular - HTTP Client
- Angular - Request
- Angular - Response
- Angular - GET
- Angular - POST
- Angular - PUT
- Angular - DELETE
- Angular - JSONP
- Angular - CRUD Operations Using HTTP
- Angular Modules
- Angular - Introduction to Modules
- Angular - Root Module
- Angular - Feature Module
- Angular - Sharing Module
- Angular - Routing Module
- Angular - NgModules
- Angular Animation
- Angular - Animations
- Angular Service Workers & PWA
- Angular - Service Workers & PWA
- Angular Testing
- Angular - Testing Overview
- Angular Design Patterns
- Angular - Design Patterns
- Angular - Lazy Loading
- Angular - Singleton Pattern
- Angular - Observer Pattern
- Angular Libraries
- Angular - Libraries
- Angular - Angular Material
- Angular - PrimeNG
- Angular - RxJS
- Angular Advanced
- Angular - Signals
- Angular - Authentication & Authorization
- Angular - Internationalization
- Angular - Standalone Component
- Angular - Accessibility
- Angular - Web Workers
- Angular - Server Side Rendering
- Angular - Ivy Compiler
- Angular - Building with Bazel
- Angular - Backward Compatibility
- Angular - Reactive Programming
- Angular Tools
- Angular - CLI
- Angular Material UI Elements
- Angular - Paginator
- Angular - Datepicker
- Angular - Select Drop-down
- Angular Miscellaneous
- Angular - Third Party Controls
- Angular - Configuration
- Angular - Displaying Data
- Angular - Decorators & Metadata
- Angular - Basic Example
- Angular - Error Handling
- Angular - Testing & Building a Project
- Angular - Lifecycle Hooks
- Angular - User Input
- Angular - What's New?
- Angular Useful Resources
- Angular - Quick Guide
- Angular - Useful Resources
- Angular - Discussion
Angular - Routing Module
This chapter will discuss the "Angular Routing Module". It is quite similar to routing concepts, which we have already covered in the previous angular chapter.
Routing Module in Angular
In Angular, a Routing Module is a special module that handles the configuration and management of routes within an application. It defines the routes (paths) for the various components to navigate between views or components.
You can define application routing logic separately from the rest of the application, which will react to the user interaction.
Important Points of Routing Module
Following are a few important points about the Routing Module you must know:
- In older angular version, the CLI automatically created a routing module named as app-routing.module.ts while creating a new application, where you can define routes to navigate between various components.
- In latest angular version, the CLI does not automatically create a routing module but instead creates a file named app.routes.ts to define the routes but it is not a routing module.
- This file contains the route configuration array, which is then imported into a separate routing module, such as AppRoutingModule, to configure the RouterModule.
Older Angular Version:
Latest Angular Version:
Important! The following creation steps of the routing module will be followed as per the "latest Angular version".
Creating Routing Module in Angular
To create a routing module in your existing or new Angular application, follow and implement the steps given below:
Step 1: Open or Create a new Angular project
Open any existing angular project in your preferred code editor (e.g., vs code) or create a new application by running the following command:
ng new myapp
Step 2: Create app.routes.ts
Create a new file named app.routes.ts within the src/app folder (if it is not already created by Angular CLI):
src/app -> app.routes.ts
Step 3: Update the app.routes.ts
Open the app.routes.ts file in your code editor. If it is empty, add the given code below:
import { Routes } from '@angular/router'; export const routes: Routes = [ ];
Step 4: Create Standalone Components
Create two standalone components named Home and About using the following commands:
ng generate component Home ng generate component About
Step 5: Define routes
Update the app.routes.ts to define the routing paths for the new components:
import { Routes } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; export const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent} ];
How to use Routing Module in Angular?
We have created a routing module and defined the routes for the Home and About components to navigate when the URL changes. As the routing module is ready to use, we need to import some necessary modules, directives, and components to use it in our application.
Follow the steps given below to make it ready for use in your application:
Step 1: Configure routing in AppConfiguration
Open the app.config.ts file in your code editor import routes and add the same in the providers array:
import { ApplicationConfig } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { providers: [provideRouter(routes)] };
Step 2: Update the app.component.ts
Open the app.component.ts file in your code editor import the necessary modules (e.g., RouterModule, RouterOutlet directive, and components), and add the same within the imports array:
import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import {RouterModule, RouterOutlet } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; @Component({ selector: 'app-root', standalone: true, imports: [ CommonModule, RouterOutlet, RouterModule, HomeComponent, AboutComponent ], templateUrl: './app.component.html', styleUrl: './app.component.css' }) export class AppComponent { title = 'myApp'; }
Step 3: Update the app.component.html
Open the app.component.html file and update it with the following code:
<h3>Routing Module Example</h3> <a routerLink="home">Home</a> <a routerLink="about">About</a> <hr> <router-outlet></router-outlet>
Step 4: Update the app.component.css
Open the app.component.css file and place the code below:
a{ text-decoration: none; margin: 10px 10px; background-color: green; padding: 10px ; color: white; } hr{ margin: 20px 0px; padding: 2px; }
Step 5: Run the Application:
Now open your preferred browser (e.g., chrome) and navigate to the localhost:4200 URL to see the output:
