Angular Child Routes
Learn how to create nested routes in Angular using children and display child components with router-outlet.
What are Child Routes?
Child routes are routes defined inside another route. They are also called nested routes.
They are useful when one main page contains multiple related sections or views.
For example, a course dashboard can contain child pages such as Overview, Topics and Assignments.
Child Route Flow
Important Concepts
Parent Route
The main route that contains one or more child routes.
Children
The children property defines nested routes.
router-outlet
It provides the location where the child component is displayed.
Nested URL
A child route becomes part of the parent route URL.
Defining Child Routes
Angular allows child routes to be defined using the children property of a route.
Each child route contains its own path and component.
const routes: Routes = [
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: 'overview',
component: OverviewComponent
},
{
path: 'topics',
component: TopicsComponent
}
]
}
];
Nested URL Structure
/courses/overview
/courses/topics
Route Structure
Parent Route: /courses
Child Route: /overview
Complete URL: /courses/overview
Using router-outlet
The parent component needs a router-outlet where Angular can display the selected child component.
Without the outlet, the child route has no location in the parent template where it can be rendered.
<h2>
CIIT Training Institute Courses
</h2>
<nav>
<a routerLink="overview">
Overview
</a>
<a routerLink="topics">
Topics
</a>
</nav>
<router-outlet></router-outlet>
Parent and Child Relationship
Navigating to Child Routes
Child routes can be accessed from the parent component using relative route links.
The child path is written without repeating the complete parent URL.
<a routerLink="overview">
Course Overview
</a>
<a routerLink="topics">
Course Topics
</a>
Programmatic Child Navigation
Child routes can also be opened programmatically using the Angular Router.
Relative navigation is useful when the current component is already inside the parent route.
import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-courses',
templateUrl: './courses.component.html'
})
export class CoursesComponent {
constructor(
private router: Router,
private route: ActivatedRoute
) {
}
openTopics() {
this.router.navigate(
['topics'],
{
relativeTo: this.route
}
);
}
}
<button
type="button"
(click)="openTopics()">
View Topics
</button>
CIIT Training Institute Example
const routes: Routes = [
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: 'overview',
component: CourseOverviewComponent
},
{
path: 'topics',
component: CourseTopicsComponent
},
{
path: 'contact',
component: CourseContactComponent
}
]
}
];
<h2>
CIIT Training Institute
</h2>
<nav>
<a routerLink="overview">
Course Overview
</a>
<a routerLink="topics">
Course Topics
</a>
<a routerLink="contact">
Course Contact
</a>
</nav>
<router-outlet></router-outlet>
CIIT Training Institute
/courses/overview → Course Overview
/courses/topics → Course Topics
/courses/contact → Course Contact
Nested Child Routes
Child routes can themselves contain additional child routes.
This creates multiple levels of routing when an application has a more complex structure.
const routes: Routes = [
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: 'fullstack',
component: FullStackComponent,
children: [
{
path: 'topics',
component: TopicsComponent
}
]
}
]
}
];
Parent Routes vs Child Routes
| Feature | Parent Route | Child Route |
|---|---|---|
| Location | Top-level route | Inside a parent route |
| Configuration | Defined directly in routes | Defined inside children |
| URL Example | /courses |
/courses/topics |
| Rendering | Application outlet | Parent's router-outlet |
Important Points
Define nested routes using the children property.
The parent component should contain router-outlet for displaying child views.
Child routes are useful for grouping pages that belong to the same parent feature.
Relative links and navigation can make nested route navigation easier to maintain.
Child Route Best Practices
- Use child routes for logically related pages.
- Keep route hierarchy simple and meaningful.
-
Place
router-outletin the parent component where child content should appear. - Use relative navigation when working inside nested routes.
- Avoid creating unnecessary levels of nested routes.
- Use clear and descriptive child route names.
Summary
Angular child routes allow an application to create nested navigation structures.
The children property defines nested routes, while router-outlet provides the location where the child component is rendered.
Child routes are useful for organizing related sections such as course overview, topics and contact pages. In the next topic, we will learn about Route Guards.