Learn Routing In Angular 8

With the help of routing, we can navigate from one component to another component (i.e), we can navigate from one page to another page.

In this article, let's explore what Routing is and the uses of the router in Angular applications. Routing is a kind of process in which we can divide the UI of a web application with the help of a URL. With the help of Routing, we can build modern single-page applications in which the entire page is loaded only once, but with multiple views with the same page. I hope everyone will be aware of the concept of Single Page Applications. If not, we can take a quick overview. In Single Page Applications, once the web page has been loaded, the web application will interact with the users dynamically without loading the entire page every time when a user requests a specified data element.
 
We can also state about routing in this way too like, by using the Routing we can navigate from one component to another component within the same application. When an application has been loaded for the first time, the homepage or index page will be displayed first on the view, then based on the request from the user the page will be navigated from one to another.
 

Route and the path

 
The Route is like an object in which the information about the component is to be mapped with a specific path.
 
An example of routing with representing with the ID parameter,
 
{path: ‘/:id’, component: ComponentName}
 

Setting-up

 
Step 1
 
The first step is to create a new Angular project by using the following command in the terminal
 
ng new routing
 
Learn About Routing In Angular 8
 
Step 2
 
Now open the Angular project with the Visual Studio code, and open the app-routing.module.ts file and import the RouterModule inside of it.
 
Learn About Routing In Angular 8
 
Step 3
 
For a demonstration of routing in this article, we will create two components such as Home and Login using the following command in the terminal.
 
ng generate a component home
ng generate component login
 
Learn About Routing In Angular 8
 
Step 4
 
For a rich expression of UI in this article, we will use bootstrap for rich user interactive page so, for that switch to the terminal and execute the following command and then replace the following code inside the index.html file, 
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">  
  2. <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>  
npm install bootstrap –save
 
Learn About Routing In Angular 8
 
Step 5
 
So far, we have created two components named home and log in. Now it’s time to add the routes in app-routing.module.ts, now open app-routing.module.ts file and add the following code inside of it.
 
Learn About Routing In Angular 8
 
Step 6
 
So far, we have created two components and installed bootstrap Module, now open app.component.html file and replace the following code as mentioned below.
  1. <div class="container-fluid">  
  2.     <nav class="navbar navbar-expand-lg navbar-light bg-light">  
  3.         <a class="navbar-brand" href="#">Simple example for Angular routing</a>  
  4.         <div class="collapse navbar-collapse" id="navbarNav">  
  5.             <ul class="navbar-nav">  
  6.                 <div>  
  7.                     <li class="nav-item active">  
  8.                         <nav>  
  9.                             <a routerLink="/home" routerLinkActive="active">Home</a>  
  10.                         </nav>  
  11.                     </li>  
  12.                 </div>  
  13.                 <br>  
  14.                 </ul>  
  15.                 <li class="nav-item">  
  16.                     <nav>  
  17.                         <a routerLink="/login" routerLinkActive="active">Login</a>  
  18.                     </nav>  
  19.                 </li>  
  20.             </div>  
  21.         </nav>  
  22.         <router-outlet></router-outlet>  
  23.     </div>  
Learn About Routing In Angular 8
 
Step 7
 
Now comes the main part of our output, for that open the terminal and execute the following command to view the output in a browser.
 
ng serve -open
 
The program will be compiled successfully and we can view the output in a web browser. The output represents the output screen with some lines of content and pagination icons. We can switch from one page to another page so that the content of the consent page will be displayed in the browser.
 
Learn About Routing In Angular 8
 
Learn About Routing In Angular 8
 

Summary

 
In this article, we have seen about an overview of Angular Routing and navigating from one page to another page. I hope this article will be useful for you and in my next article we will be seeing about Angular services.