Introduction
In this article, we are going to learn the relative navigation in the Angular application. While building the application with the use of absolute path we pin the same link everywhere we need it in that path. So when we change the parent route then we have to change the path to reflect the links used outside also. So by the use of relative path you are making your links free that are relative to the current URL segment. Your feature area of routing will be the same even if you change the route for the parent. You are just making your link free even if you change the parent route path.
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 the route parameter in Angular with paramMap.
The path we are using so far was absolute and it begins with ‘/’ forward slash. If you take a look at the path used in student-list.component.html we used.
- //studentDetails
And in student-details.component.ts we used.
- gotoStudentList(){
- this.router.navigate(['/studentList', this.student])
- }
/studentList for navigation from one view to another. So when we are using an absolute path we are lacking the flexibility of routing in the application.
Let us modify out the application for better understanding.
Open app-routing.module.ts and change the routes value as below:
- const routes:Routes = [
- {path: '', redirectTo : '/student-list', pathMatch : 'full'},
- {path: 'student-list', component : StudentListComponent},
- {path: 'student-list/:id/:name/:marks', component : StudentDetailsComponent},
- {path: 'studentMarks', component : StudentMarksComponent},
- {path: 'addStudent', component : StudentComponent},
- {path: "**", component : NotFoundComponent}
- ];

Sani YadavPosted Apr 18, 2020, 12:28 PM
Nice article
Hamid KhanPosted Nov 27, 2018, 11:19 PM
Good explanation..............,