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
- HTML, CSS, and JS
- Basics of TypeScript
- Route Parameter in Angular with paramMap
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.
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.
- <p>
- Address view: This is the address view for the student.
- </p>
Open student-fees.component.html and add the following lines.
- <p>
- Fees view: This is fees view for the student.
- </p>
Open app-routing.component.ts and change the value of routes as below.
- const routes:Routes = [
- {path: '', redirectTo : '/student-list', pathMatch : 'full'},
- {path: 'student-list', component : StudentListComponent},
- {path: 'student-list/:id/:name/:marks', component : StudentDetailsComponent,
- children : [
- {path: 'fees', component : StudentFeesComponent},
- {path: 'address', component : StudentAddressComponent}
- ]},
- {path: 'studentMarks', component : StudentMarksComponent},
- {path: 'addStudent', component : StudentComponent},
- {path: "**", component : NotFoundComponent}
- ];
Open student-details.component.ts and add the below content.
- <h2>Selected Student Details</h2>
- <p>Id : <b>{{student.id}}</b></p>
- <p>Name : <b>{{student.name}}</b></p>
- <p>Marks : <b>{{student.marks}}</b></p>
- <button (click)="showFees()">Fees</button>
- <button (click)="showAddress()">Address</button>
- <router-outlet></router-outlet>
- <div>
- <button (click)="gotoStudentList()">Back</button>
- </div>

Sani YadavPosted Apr 18, 2020, 12:26 PM
Basic but useful