Overview Of Child Routes In Angular

Introduction

In this article, we are going to use child routes in our navigation application. Child routes are used in such scenarios when we require some routes to be rendered or viewed inside other routes. So, when we have such requirements where we have the submodule view kind of requirement, we can go with child view. This is just like the normal view but renders inside some parent view.

Prerequisites

Before we start, please go through the creation of an application using a route parameter in Angular with paramMap.

Let us start with the code for better understanding. We will add two Views - fees and address - to be viewed on studentDetails View when we click on the corresponding buttons respectively.

We are going to create two components - StudentAddress and StudentFees component.

Child Routes In Angular 

These two new Views of the above added components will be used as child view for the studentDetails component view.

Open student-address.component.html and add the below code.

  1. <p>  
  2.   Address view: This is the address view for the student.  
  3. </p>  

Open student-fees.component.html and add the following lines.

  1. <p>  
  2.   Fees view: This is fees view for the student.  
  3. </p>  

Open app-routing.component.ts and change the value of routes as below.

  1. const routes:Routes = [  
  2.     {path: '', redirectTo : '/student-list', pathMatch : 'full'},  
  3.     {path: 'student-list', component : StudentListComponent},  
  4.     {path: 'student-list/:id/:name/:marks', component : StudentDetailsComponent,  
  5.         children : [  
  6.             {path: 'fees', component : StudentFeesComponent},  
  7.             {path: 'address', component : StudentAddressComponent}  
  8.         ]},  
  9.     {path: 'studentMarks', component : StudentMarksComponent},  
  10.     {path: 'addStudent', component : StudentComponent},  
  11.     {path: "**", component : NotFoundComponent}  
  12. ];  

Open student-details.component.ts and add the below content.

  1. <h2>Selected Student Details</h2>  
  2. <p>Id : <b>{{student.id}}</b></p>  
  3. <p>Name : <b>{{student.name}}</b></p>  
  4. <p>Marks : <b>{{student.marks}}</b></p>  
  5. <button (click)="showFees()">Fees</button>  
  6. <button (click)="showAddress()">Address</button>  
  7. <router-outlet></router-outlet>  
  8. <div>  
  9.     <button (click)="gotoStudentList()">Back</button>  
  10. </div>  

Your Child View will render under <router-outlet>. Open student-details.component.ts and add the below code.

  1. import { Component, OnInit } from '@angular/core';  
  2. import { StudentService } from '../student.service';  
  3. import { ActivatedRoute, Router } from '@angular/router';  
  4.   
  5. @Component({  
  6.   selector: 'app-student-details',  
  7.   templateUrl: './student-details.component.html',  
  8.   styleUrls: ['./student-details.component.css']  
  9. })  
  10. export class StudentDetailsComponent implements OnInit {  
  11.   
  12.   public student = {  
  13.      id: "",   
  14.      name: "",   
  15.      marks: ""  
  16.     };  
  17.   constructor(private route : ActivatedRoute, private router : Router) {   
  18.   }  
  19.   
  20.   ngOnInit() {  
  21.     this.student.id = this.route.snapshot.paramMap.get('id');  
  22.     this.student.name = this.route.snapshot.paramMap.get('name');  
  23.     this.student.marks = this.route.snapshot.paramMap.get('marks');  
  24.   }  
  25.   
  26.   gotoStudentList(){  
  27.     this.router.navigate(['../../../',this.student], {relativeTo: this.route});  
  28.   }  
  29.   
  30.   showFees(){  
  31.     this.router.navigate(['fees'], {relativeTo: this.route});  
  32.   }  
  33.   
  34.   showAddress(){  
  35.     this.router.navigate(['address'], {relativeTo: this.route});  
  36.   }  
  37. }  

Now, we are all ready to test the Child View in our application.

Run the application.

Child Routes In Angular 

Click on any record.

Child Routes In Angular 

Click on the "Fees" button to see the Child View.

Child Routes In Angular 

You can see the URL changes to http://localhost:4200/student-list/1002/Imran/80/fees

Click on the "Address" button to see the Child View.

Child Routes In Angular 

You can see the URL changes to http://localhost:4200/student-list/1002/Imran/80/address

What we have done above,

  1. Created two Child Views - Fees and Address
  2. Added the children path (fees and address) on studentDetails component path on our route's component that we created in app-routes.module.ts.
  3. Added <router-outled> tag to handle and render the Child View inside the studentDetails View.
  4. Also, added the clickable button to navigate to the child view with its handler function.

So, these are the child routes (fees and address) that are displayed within the studentDetails Component View. You can also use the wildcard child route to handle the unknown URL.