Angular Route Configuration
Angular Route Configuration defines the URL paths of an application and specifies which component should be displayed when a particular route is activated.
What is Route Configuration?
Route configuration is the process of defining Angular routes using a Routes array.
Each route can contain a path and a component. Angular uses this configuration to determine which component should be displayed for the current URL.
Simple Definition
Route Configuration connects a URL path with an Angular component.
Route Configuration Flow
1. Creating a Routes Array
Angular routes are commonly stored in a Routes array.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent
}
];
How it works
When the user navigates to /courses, Angular matches the route and displays CoursesComponent.
2. Understanding a Route Object
A route is represented by an object containing configuration properties such as path and component.
{
path: 'courses',
component: CoursesComponent
}
path
Defines the URL segment used for the route.
'courses'
component
Defines the component displayed by the route.
CoursesComponent
children
Defines nested child routes.
children
canActivate
Defines route activation guards.
canActivate
3. Configuring Multiple Routes
An Angular application can contain multiple routes for different components.
export const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'courses',
component: CoursesComponent
},
{
path: 'contact',
component: ContactComponent
}
];
/ → HomeComponent
/courses → CoursesComponent
/contact → ContactComponent
4. Configuring the Default Route
An empty path can represent the default route of an application.
{
path: '',
component: HomeComponent
}
Meaning
The empty path represents the root URL of the application.
5. Redirecting a Route
A route can redirect the user from one path to another path using redirectTo.
export const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: 'home',
component: HomeComponent
}
];
6. Configuring a Wildcard Route
A wildcard route can handle URLs that do not match another configured route.
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent
},
{
path: '**',
component: NotFoundComponent
}
];
7. Parameterized Route Configuration
Route parameters allow part of the URL to contain dynamic data.
{
path: 'course/:id',
component: CourseDetailsComponent
}
/course/101
Meaning
The :id segment represents dynamic route information.
8. Configuring Child Routes
Child routes can be defined inside a parent route using the children property.
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: 'details',
component:
CourseDetailsComponent
}
]
}
];
9. Configuring Route Guards
A route can use guards to control whether navigation to a route should be allowed.
{
path: 'admin',
component: AdminComponent,
canActivate: [
authGuard
]
}
Use Case
Guards are commonly used to protect application areas that require authentication or other conditions.
10. Lazy Loading Route Configuration
Routes can also be configured to load feature components or route configurations only when they are needed.
{
path: 'admin',
loadComponent: () =>
import('./admin.component')
.then(m => m.AdminComponent)
}
Benefit
Lazy loading can keep larger applications modular by loading feature code when that route is needed.
11. Common Route Configuration Patterns
Default Route
Defines the root application page.
path: ''
Static Route
Uses a fixed path such as courses.
path: 'courses'
Dynamic Route
Uses a parameter in the route path.
path: 'course/:id'
Wildcard Route
Handles unmatched URLs.
path: '**'
12. Connecting Routes with RouterOutlet
The configured component needs a router-outlet placeholder where Angular can render the routed view.
<nav>
<a routerLink="/">
Home
</a>
<a routerLink="/courses">
Courses
</a>
</nav>
<router-outlet>
</router-outlet>
Important
The router-outlet is the location where Angular renders the component associated with the active route.
Route Configuration Quick Reference
| Property | Purpose | Example |
|---|---|---|
| path | Defines the URL path. |
path: 'courses'
|
| component | Defines the component displayed by the route. |
component: CoursesComponent
|
| redirectTo | Redirects one route to another. |
redirectTo: 'home'
|
| children | Defines nested routes. |
children: []
|
| canActivate | Controls route activation. |
canActivate: [authGuard]
|
| loadComponent | Loads a component dynamically. |
loadComponent: () => import(...)
|
Complete CIIT Training Institute Example
Suppose the CIIT Training Institute Angular application has Home, Courses, Course Details and Contact pages. We can define routes for each page and use RouterOutlet to display them.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'courses',
component: CoursesComponent
},
{
path: 'course/:id',
component:
CourseDetailsComponent
},
{
path: 'contact',
component: ContactComponent
},
{
path: '**',
component: NotFoundComponent
}
];
<nav>
<a routerLink="/">
Home
</a>
<a routerLink="/courses">
Courses
</a>
<a routerLink="/contact">
Contact
</a>
</nav>
<router-outlet>
</router-outlet>
/ → CIIT Home
/courses → Course List
/course/101 → Course Details
/contact → Contact Page
Important Points About Route Configuration
- Routes are defined using a Routes array.
- The path defines the URL segment.
- The component property identifies the component displayed for the route.
- redirectTo can redirect one path to another.
- children can be used for nested routes.
- Wildcard routes can handle unmatched URLs.
Route Configuration Best Practices
- Keep route paths meaningful and easy to understand.
- Use consistent naming for related routes.
- Keep wildcard handling at the appropriate place in the route configuration.
- Use child routes when a feature contains logically nested views.
- Use route guards for protected application areas.
- Keep large applications organized into clear routing structures.
Summary
Angular Route Configuration defines the relationship between application URLs and Angular components.
Important configuration properties include path, component, redirectTo, children, canActivate, and loadComponent.