Angular Router

In Previous topic we have looked into HTTP Client in Angular, In this topic we will look routing mechanism in Angular. we will set up and configure router.

Routing ➤➤

Routing allow us to navigate from one component to another based on action taken by users.

The Angular router module is a separate module which is present in @angular/router package.

Configure Angular Router :

Angular Router is configured automatically when we create a new application using Angular CLI. If you want to do it manually you need to follow below steps.

1. Base href tag:

you have to place <base href="/"> inside index.html within the head tag as shown below.
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>SchoolManagementSystem</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

2. Defining Routes :

You can create a module if not present in side app folder where you can define all the routes. After that you can create array of route object each route maps the path to the component.
Each route can have different attribute like path, component, pathMatch, children, redirectTo etc. After defining the routes you have to register the routes, import the routing module from @angular/router library. Sample code shown below

Here you can see that in routes first path is ' ' and component is LoginComponent which means if you type http://localhost:4200/ it will route to http://localhost:4200/login i.e. login page.
Similarly in last route you can see we have used special string ** which means if the requested URL doesn't match any of your defined path or route it will navigate to Error Page i.e. ErrorComponent. example : http://localhost:4200/xyz it will route to Error Page.

Now you have to import AppRoutingModule in your root module i.e. app.module.ts so that it will be globally accessible.

 Finally we need to tell angular where to display the view, this is done by <router-outlet> directive . we will add this directive in root component as shown below.
As we have configured our router now we will look into some more important properties in Router:

Angular Route Guard ➤➤

Angular Route guards are interfaces provided by Angular, when we implement it, it allows us to control accessibility of a route based on conditions provided in class implementation of that interface.

There are five different types of guards.
  • CanActivate
  • CanActivatedChild
  • CanDeactivate
  • canLoad
  • Resolve
For example we will configure canActivate in code, for that i have created one service using cmd "ng generate service router"


After creating RouterService we will implement canActivate interface, we will check if user is logged in then we can route otherwise it will route to login page. Code Below:


and same we have to use this in app routing module to route to login page if not logged in. code Below:


Router Link ➤➤

RouterLink is a directive on anchor tag. As in single page application we don't have different page to link, instead we have different view or component to display the users. 

<a routerLink="/homepage">
    Link that uses a string.
</a>

In above you can see we didn't use bracket for routerLink, by using this way it means we are hardcoded the string and it cannot be changed, it will be same throughout application.

When we are using bracket it means we are passing a bindable property, so this variable "homepage" could be defined inside your class and it should have value and we can change it as per requirement as this is dynamic.

<a [routerLink]="homepage">
    Link that uses a string.
</a>

Comments

Popular posts from this blog

Introduction to Angular

Bootstrap in Angular

Angular Directives