Overview Of Wildcard Routing And Path Matching In Angular

Introduction

In any Angular application, we use routing to navigate from one view to another. What happens if we don’t define a certain route and we try to navigate to that route? Well, the Angular application throws an error. So, we have to carefully define the router for navigation.

We can use wildcard routing for defining the route. With the help of wildcard, we can perform certain operations related to defining of the route for an application.

Prerequisites

Before we start, please go through the creation of an Angular application using routes.

Run the application and navigate to the below route.

http://localhost:4200/studentFees

You will get the error like below.

Wildcard Routing And Path Matching In Angular 

Let us create a new component for any incorrect route.

>ng g c not-found

Open the not-found.component.html file and add the following content.

  1. <h2>Not found!</h2>  
  2. <p>The page you are looking for is not available.</p>  

Open app-routing.module.ts and add the below code.

  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { StudentDetailsComponent } from './student-details/student-details.component';  
  4. import { StudentMarksComponent } from './student-marks/student-marks.component';  
  5. import { NotFoundComponent } from './not-found/not-found.component';  
  6.   
  7. const routes:Routes = [  
  8.     {path:'studentDetails', component : StudentDetailsComponent},  
  9.     {path: 'studentMarks', component : StudentMarksComponent},  
  10.     {path: "**", component : NotFoundComponent}  
  11. ];  
  12.   
  13. @NgModule({  
  14.     imports : [RouterModule.forRoot(routes)],  
  15.     exports: [RouterModule]  
  16. })  
  17. export class AppRoutingModule{}  

We have just added one more component - NotFoundComponent.

Open app.module.ts and make sure that NotFoundComponent is imported and added in the declaration array. Angular CLI already has done this importing of the newly added component.

Run the application and navigate to the below route.

http://localhost:4200/studentFees

You will get this View.

 Wildcard Routing And Path Matching In Angular

Navigate to the below route.

http://localhost:4200

Wildcard Routing And Path Matching In Angular 

It's still the same, because we have not configured the route for this. Let us create the StudentDetails as a display component for an empty path.

Open app-routing.module.ts and set the route's value as below.

  1. const routes:Routes = [  
  2.     {path: '', component : StudentDetailsComponent},  
  3.     {path:'studentDetails', component : StudentDetailsComponent},  
  4.     {path: 'studentMarks', component : StudentMarksComponent},  
  5.     {path: "**", component : NotFoundComponent}  
  6. ];  

Run the application.

Wildcard Routing And Path Matching In Angular  

The NotFoundComponent is not displaying for localhost:4200 route.

Note
Always define the path in the end of the definition of the routes.

  1. {path: "**", component : NotFoundComponent}  

If we define this path having wildcard ** at the top, then it will execute first everytime and none of your routes will come in use.

redirectTo and pathMatch properties of the route

The RedirectTo property of routes is useful in defining the route when we want a particular pattern to redirect to some route. Instead of defining the route component, we simply redirect it to another route.

When we define the redirectTo property, then we have to use a pathMatch property to tell the URL how URL Segments match with the configured route. It has two values.

  1. prefix: It tells the url - if the path is prefixed of the url then redirect to the given route.
  2. full: It tells the url - if the full path is matched, then only redirect.

Change the route value to below.

  1. const routes:Routes = [  
  2.     {path: '', redirectTo : '/studentDetails', pathMatch : 'prefix'},  
  3.     {path:'studentDetails', component : StudentDetailsComponent},  
  4.     {path: 'studentMarks', component : StudentMarksComponent},  
  5.     {path: "**", component : NotFoundComponent}  
  6. ];  

The empty string value can be the prefix of every route. So, it will show the studentDetails component everytime.

Change the route value to something like below.

  1. const routes:Routes = [  
  2.     {path: '', redirectTo : '/studentDetails', pathMatch : 'full'},  
  3.     {path:'studentDetails', component : StudentDetailsComponent},  
  4.     {path: 'studentMarks', component : StudentMarksComponent},  
  5.     {path: "**", component : NotFoundComponent}  
  6. ];  

Now, if the full URL is blank, then only it will redirect to the studentDetails route.

So, the prefix value is not very useful for us but using the full match, we can achieve what we wanted.