In this section, we will explore child routes in Angular. Child routes allow you to create nested routes within a parent route, enabling more complex and hierarchical navigation structures in your Angular applications.
Key Concepts
- Parent and Child Routes: Understanding the relationship between parent and child routes.
- Nested Routes: How to define and configure nested routes.
- RouterOutlet: Using multiple
<router-outlet>
directives to display nested routes. - Route Configuration: Setting up the route configuration for child routes.
- Parent and Child Routes
Parent routes are the main routes in your application, while child routes are nested within these parent routes. This hierarchical structure allows for more organized and manageable routing.
Example Structure
- Nested Routes
To define nested routes, you need to configure the children
property in the route configuration of the parent route.
Example
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { ParentComponent } from './parent/parent.component'; import { Child1Component } from './child1/child1.component'; import { Child2Component } from './child2/child2.component'; const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
In this example, ParentComponent
is the parent route, and Child1Component
and Child2Component
are the child routes.
- RouterOutlet
To display the child routes, you need to use the <router-outlet>
directive in the parent component's template.
Example
<!-- parent.component.html --> <h2>Parent Component</h2> <nav> <a routerLink="child1">Child 1</a> <a routerLink="child2">Child 2</a> </nav> <router-outlet></router-outlet>
In this example, the <router-outlet>
directive in ParentComponent
will display the child components based on the active route.
- Route Configuration
Ensure that your route configuration is correctly set up to handle child routes. The children
property is used to define the nested routes.
Example
const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ];
Practical Exercise
Task
- Create a new Angular component named
ParentComponent
. - Create two new Angular components named
Child1Component
andChild2Component
. - Configure the routes to nest
Child1Component
andChild2Component
underParentComponent
. - Add navigation links in
ParentComponent
to navigate toChild1Component
andChild2Component
. - Use
<router-outlet>
inParentComponent
to display the child components.
Solution
- Create Components
- Configure Routes
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { ParentComponent } from './parent/parent.component'; import { Child1Component } from './child1/child1.component'; import { Child2Component } from './child2/child2.component'; const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
- Add Navigation Links
<!-- parent.component.html --> <h2>Parent Component</h2> <nav> <a routerLink="child1">Child 1</a> <a routerLink="child2">Child 2</a> </nav> <router-outlet></router-outlet>
Summary
In this section, we learned about child routes in Angular. We covered the following key points:
- The concept of parent and child routes.
- How to define and configure nested routes using the
children
property. - Using the
<router-outlet>
directive to display child components. - Practical exercise to reinforce the learned concepts.
By understanding and implementing child routes, you can create more complex and hierarchical navigation structures in your Angular applications. In the next section, we will explore route guards to protect routes and manage access control.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces