Routing And Navigation With Angular 5

Angular routing helps navigation across the application from one view to another view. It also allows us to maintain the state, implement modules, and load the modules based on the role of the user. Let’s see in this article how we configure the routing and some basic concepts with it. For this let’s add 3 component in the application Namely Home, Employee and Department our goal is to load the following pages below is the routing table for the configuration,

PathURLComponent
/Localhost:4200Employee
/EmployeeLocalhost:200/EmployeeEmployee
/DepartmentLocalhost:4200/DepartmentDepartment

Basic Setup

In this let’s see the basic import that we need to do so as to make the routing work in our application. To use the routing module we need to import some package in the application module which can be like this

import {Routes,RouterModule} from '@angular/router';

Route Configuration

Now next step is to map the URL to a component that can be shown on the page which is done by using the Route Configuration which is nothing but the array of the path how we can do this. Let's add new class file and name it as ApplicationRoutes.ts. The code snippet for the same is like below.

  1. import {Routes} from '@angular/router';  
  2. import { EmployeeComponent } from './employee/employee.component';  
  3. import { DepartmentComponent } from './department/department.component';  
  4.   
  5. export const routes: Routes = [  
  6.     { path: '', component: EmployeeComponent },  
  7.     { Path: 'employee', component: EmployeeComponent },  
  8.     {Path: 'Department', component: DepartmentComponent }  
  9.     ]  

There are two main part which are,

  1. path: It describe the URL this route handle
  2. component: This is the name of the component we want to display when URL in the Browser matches the Route
  3. Routes: This is the typescript array of the Routes

Next thing what we need to do is we need to import these routs in the application we can do this by adding lines RouterModule.forRoutes(routes) into the imports section of the Application Module

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import {RouterModule} from '@angular/router';  
  4. import { AppComponent } from './app.component';  
  5. import { HomeComponentComponent } from './home-component/home-component.component';  
  6. import { EmployeeComponent } from './employee/employee.component';  
  7. import { DepartmentComponent } from './department/department.component';  
  8.   
  9. import {routes} from './ApplicationRoutes'  
  10.   
  11. @NgModule({  
  12.   declarations: [  
  13.     AppComponent,  
  14.     HomeComponentComponent,  
  15.     EmployeeComponent,  
  16.     DepartmentComponent  
  17.   ],  
  18.   imports: [  
  19.     BrowserModule  
  20.    ,  
  21.     RouterModule.forRoot(routes, {useHash: true})  
  22.   ],  
  23.   providers: [],  
  24.   bootstrap: [AppComponent]  
  25. })  
  26. export class AppModule {   
  27.   
  28.     
  29. }  

Another thing that we need to consider is the <router-outlet></router-outlet>

Directive that we have now question is what it does?

Remember we have mapped the routes to the link in our application but where we want to display them? Here It tells angular where it should insert the component specified in the route table so in our case it will be app.component and code snippet for that will be like below. 

  1. <div>  
  2.  </div>  
  3. <table border="1">  
  4.     <tr>  
  5.       <td>  
  6.          <nav class="navbar navbar-light bg-faded">  
  7.     
  8.   <ul class="nav navbar-nav">  
  9.       <li class="nav-item active">  
  10.           <a class="nav-link" href="#">Home</a>  
  11.         </li>  
  12.     <li class="nav-item active">  
  13.       <a class="nav-link" [routerLink]="['employee']">Employee</a>  
  14.     </li>  
  15.     <li class="nav-item">  
  16.       <a class="nav-link" [routerLink]="['Department']">Departement</a>  
  17.     </li>  
  18.   </ul>  
  19. </nav>  
  20.   
  21.       </td>  
  22.       <td>  
  23.         <router-outlet></router-outlet>  
  24.       </td>  
  25.     </tr>  
  26.   </table>  

When we run and see the application we can see the output,

output

Whenever we click on the link at the left hand side then corresponding component will be loaded .

Next section that we need to understand is the Redirect and the situation where our URL doesn’t map to any of the component in these situations we generally redirect them to one of the components.

For this, we need to just change in the Route configuration like below,

  1. import {Routes} from '@angular/router';  
  2. import { EmployeeComponent } from './employee/employee.component';  
  3. import { DepartmentComponent } from './department/department.component';  
  4. import { HomeComponentComponent } from './home-component/home-component.component';  
  5.   
  6. export const routes: Routes = [  
  7.     { path: '',redirectTo:'Home',pathMatch:'full' },  
  8.     { path: 'Home',component:HomeComponentComponent },  
  9.     { path: 'employee', component: EmployeeComponent },  
  10.     { path: 'Department', component: DepartmentComponent },  
  11.      { path: '**', component: HomeComponentComponent }  
  12.       
  13.    ]  

redirect To

This property specifies the path where we want to navigate when a user enters this URL.

** : This specifies if no match happens where to redirect in our case we are redirecting to the Home component

This is about basic routing with the Angular. Let's see the Parametrized routes and Route guards in next lectures.

References

  1. https\\Angular.io
  2. https\\rangle.io

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now