Routing and Navigation in Angular

 routing1

 

Angular is a SPA(Single page Application). 

In Single page application we change what user see by showing hiding the portion of the display corresponding to a component.

In this way we avoid requesting server to load a page on navigation.

This is developed for better user experience. In this we load components based the user action and load only corresponding component by this we avoided server page rendering and achieved better user experience.

To handle this navigation we use Angular Router. This will read the URL and provide the corresponding component defined in Router for that URL.

In this Blog we will learn about angular Routing and navigation and passing parameters in navigation and reading parameters in second component.

Topics will cover is.

  1. Creating Angular application 
  2. Create Components
  3. Adding Routing Module
  4. Define all Routes
  5. Add Navigation for Menus
  6. Wildcards

1. Creating Angular application :

We have created a blog how to create angular application. Please follow that and create project.

https://webdevelopmentknowledge.blogspot.com/2021/02/create-your-first-angular-app-using-vs-code.html

Command to create angular application

ng new my-firstAngular-app

after that add angular routing ? y/N - select N so that we will only configure Route Module and will get more understanding on this.

first1

Go to Project by cd my-firstAngular-app

and code . command to open it in visual studio code.

2. Create Components:

Open app.component.html and delete everything.

We will create 2 Components to navigate from one to another to test Routing.

Command to create component:

ng generate component {component name}

here we will create Home and ContactUs and AboutUs

routing2

routing3


we have successfully create 3 components.

3. Adding Routing Module

Command to create component:

ng generate module {module name}



routing4

Now your application structure will be as shown in figure.

routing5

Import App routing module in app module .

routing7


4. Define all Routes:

Open app-routing module 

 Add all the components:

import { HomeComponent } from  '../home/home.component';

import { ContactUsComponent } from '../contact-us/contact-us.component';

import { AboutUSComponent } from '../about-us/about-us.component';

Add Route Modules

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

then add this below for imports and exports

const routes:Routes=[

  { path: 'Home', component: HomeComponent },

  { path: 'Contactus', component: ContactUsComponent },

  { path: 'Aboutus', component: AboutUSComponent },

];

@NgModule({

  declarations: [],

  imports: [

    CommonModule,RouterModule.forRoot(routes)

  ],

  exports:[RouterModule]

})


after adding all this app-routing.module will look as below.

routting10


5. Add Navigation for Menus:

Now open app.component.htmtl  and delete if any code it their and paste below code.

<router-outlet> is used to show the component related to that route.

Code:

<h1>Angular Router App</h1>

<!-- This nav gives you links to click, which tells the router which route to use (defined in the routes constant in  AppRoutingModule) -->

<nav>

  <ul>

    <li><a routerLink="/Home" routerLinkActive="active">Home</a></li>

    <li><a routerLink="/Contactus" routerLinkActive="active">Contact US</a></li>

    <li><a routerLink="/Aboutus" routerLinkActive="active">About US</a></li>

  </ul>

</nav>

<!-- The routed views render in the <router-outlet>-->

<router-outlet></router-outlet>

routing15

Then go to command prompt to this specific folder and run ng serve command to run the angular project.

routing11

Open the port specific in cmd.

routing15

Then Click on Home:

routing21
Then Click on Contact US:
routing 22

Then Click on About US:
routing23

we have successfully achieved routing in Angular

Wildcards:

Our Application should handle when user try to navigate to a URL and that URL not exist or he is trying wrong URL. We Should show Appropriate page in this case.

To handle this we have wildcard.

add new component saying pagenottfound

routing31

Add import for page not found in app-routing:
import { PageNotFoundComponent } from '../page-not-found/page-not-found.component';

Include 2 more paths in routes. RedirectTo will happen when the URL full matches:

{ path: '',   redirectTo: 'Home', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }, 

routing 24

Now open command prompt and run ng serve

and open port url:

routing31



Now application will directly open Home instead of base URL because if blank we used redirect to Home.

In age not found html i added 

<h1>We are in 404 error Page.</h1>

Now I tried using payment URL which is not exist Hence it redirected me to page not found component.

routing 34

Thanks for reading.

Project is available in stackblitz

https://stackblitz.com/edit/angular-ivy-mqzhyi?file=src/app/app.component.ts

https://angular-ivy-mqzhyi.stackblitz.io

follow my Facebook page to get new posts update.

Popular blogs:



Post a Comment

1 Comments

  1. hire angular developers from Infomaze to build robust, secure, and reliable web applications and portals.

    ReplyDelete