Learn Angular Step By Step In Easy Way(URL Routing In Angular)

Description

 
In this article we gonna learn routing using angular 7(routing will be same for angular 2 and above versioning till now). When we talk about url routing it means redirecting from one page to another page. Every framework have his own way to do so. And it is one of the most important concept of any framework. In anuglar 7 when we start new  project using cli its automatically asked at the time of project setup for url routing(if you want to do url routing, yes or no). If you choose yes it will automatically created a file named as app-routing.module.ts, otherwise in earler version of angular you have to create this file of your own. So let's don't waste any time to start with url routing in angular.
 
Step 1
 
In my previous article i have shared the steps to start up with angular setup. If you missed that article just go through that -: 
Because you can't start with second step when you missed first step. So first what we will do is create new pages in our site. I will start from where we left last(first article). So our setup is done and our application is running. So let's create two new componet(about and contact us). We will create it with cli command to create new componet. To create a new componet, write below command -:
  1. ng generate component YOURCOMPONENTNAME  //This is the way we will create
  2. ng g c aboutus  //In short we can use command like this.
Learn Angular Step By Step In Easy Way 
 
The same we will create contactus component,
 
Learn Angular Step By Step In Easy Way
 
So we have created two componet now. But we can't access them directly because we don't described routing for those yet.  So if i run application right now it will be like below,
 
Learn Angular Step By Step In Easy Way
 
We having only this page right now on browser window, as we have already added two new component, but we can't access them till we fix routing.
 
Step 2
 
So let's add app-routing.module.ts file in our application. As i told you i am using angular 7 so this file already created in my application.and it should look like below,
 
Learn Angular Step By Step In Easy Way
 
if you don't have this file then create this file and add above code in this file. So le't add url routing code in this file. 
  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { AboutusComponent } from './aboutus/aboutus.component';  
  4. import { ContactusComponent } from './contactus/contactus.component';  
  5.   
  6. const routes: Routes = [  
  7.   { path: 'aboutus', component: AboutusComponent },  
  8.   { path: 'contactus', component: ContactusComponent }  
  9. ];  
  10.   
  11. @NgModule({  
  12.   imports: [RouterModule.forRoot(routes)],  
  13.   exports: [RouterModule]  
  14. })  
  15. export class AppRoutingModule { }  
As we can see i have added above code, and mentioned about us and contact us in Routes. In there corresponding i have added the component which i want to render or show. So we just added the configure the code for routing. Next step to show about us and contact us in menu bar with link to redirects.
 
Step 3
 
Go to app component(app component if that component mentioned in bootstraping, which mean the first componet to load) to add about us and contact us in menu bar.
  1. <div style="text-align:center">    
  2.   <h1>    
  3.     Welcome to Angular, let's start with url routing!    
  4.   </h1>    
  5. </div>    
  6. <ul>    
  7.     <li><a [routerLink]="['/aboutus']">About Us</a></li>    
  8.     <li><a [routerLink]="['/contactus']">Contact Us</a></li>    
  9. </ul>    
  10.     
  11. <div class="outer-outlet">    
  12.       <router-outlet></router-outlet>    
  13. </div>     
We have added about us and contact us in menu bar with routerLink(just like href in simple terms). We have given the same name as we written in routes file here. Name should be same else routes will not be found and redirection will not gonna work. Its case sensitive so make sure both name are same. You can also use NavigateByUrl in angular to redirect from one page to another. We will come on that in future. For now we just configure the url setting for about and contact us page. Now time to run the application,
 
Learn Angular Step By Step In Easy Way
 
Click on About Us,
 
Learn Angular Step By Step In Easy Way
Click on Contact Us,
 
Learn Angular Step By Step In Easy Way
So our routing is working fine, now we can create as much comoponet pages we want(I have implement some css here in menu). 
 
I have written another article related to URL routing and backslash in URL routing in angular, you can go through it from the below link,
So tha'ts it for now, in future we will cover our other topics related to angular as well as lazy loading and parameter url routing. Till then work hard, party harder ;)