Angular's Routing enables navigation from one view to the next as users perform application tasks.
It manages the browser's history stack and navigates to different parts of the application based on URL changes without the need for full-page reloads.
Routing Configuration: Angular's routing is configured using the RouterModule and Routes array. Routes define the mapping between URL paths and corresponding components to be displayed.
Router Outlet: The
Navigation: Navigation in Angular routing is typically performed using the RouterLink directive in HTML templates or programmatically through the Router service. RouterLink allows us to create links to different routes within the application, while the Router service provides methods for programmatic navigation.
Route Parameters: Angular routing supports route parameters, allowing us to pass data or parameters to routes dynamically. Route parameters are specified in the route path using a colon `:` followed by the parameter name, for ex:- `/order/:orderId`.
Child Routes: Angular routing allows us to define child routes within parent routes, creating nested route structures.
Lazy Loading: It reduces bundle size and allows us to load modules and their associated routes asynchronously on-demand. Lazy loading improves application performance by splitting the application into smaller bundles and loading them only when needed.
First, make sure your Angular application has the Angular Router module installed.
We can do this by including it in our Angular application's dependencies:
npm install @angular/router
Define the routes for your application in the "app-routing.module.ts" file.
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { ContactComponent } from './contact/contact.component'; const routes: Routes = [ { path: '', component: HomeComponent }, // Home route { path: 'about', component: AboutComponent }, // About route { path: 'contact', component: ContactComponent } // Contact route ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
This file is typically generated when we create a new Angular application using the Angular CLI.
ng generate component home ng generate component about ng generate component contact
Create the components for each route.
<!-- app.component.html --> <header> <nav> <a routerLink="/" routerLinkActive="active">Home</a> <a routerLink="/about" routerLinkActive="active">About</a> <a routerLink="/contact" routerLinkActive="active">Contact</a> </nav> </header> <router-outlet></router-outlet>
We can generate components using the Angular CLI:
<!-- Example usage in a template --> <a routerLink="/about" routerLinkActive="active">About</a>
In the "app.component.html", place the
Use Angular's routerLink directive to navigate between routes in your application.
Angular's router allows us to create single-page applications with multiple views.
By configuring routes and associating them with components, we can navigate between different parts of your application.
The router also supports features like route parameters, route guards, lazy loading, and more, enabling you to build complex applications with ease.