Hello friends! As you know, Angular is a single-page application, and it doesn't have multipage HTML pages to create different routes, so we require a client-side routing solution. The Angular Router helps us to create client-side routing in a few simple steps, and then we can render using routerLink
. Today, I will walk you through different options to navigate from components to various other components using code so you can navigate the user dynamically using different conditions.
Let's take a real-life and straightforward example. Suppose you have multiple roles in your application, and depending on the role, you have to decide whether or not users are authorized to access your application. If they have the authority, you'll navigate them to the home page of the application; otherwise, you'll take them to a different page, preferably with a 403 status code.
In this scenario, you will need to decide the route in one hit at the time of form submission on the login page. So what are you going to do?
Here dynamic routing comes into the picture. First, you will check the condition, and depending on that condition, you will dynamically decide the routes where a user will be sent.
Let's get started with the next section by making routes in app-routing.module.ts
.
Now we're going to make some of the components that will help us to understand Angular Router more clearly. I am going to create three components: FirstComponent
, SecondComponent
, and FirstChildComponent
, respectively. FirstChildComponent
will be used in the FirstComponent
as a child component.
The following commands will create the components.
1ng g c first --skipTests=true
2ng g c second --skipTests=true
3ng g c first-child --skipTests=true
We have successfully created the components; now, we will map it with different URI. After successfully configuring app-routing.module.ts
, it will look as follows.
1import { NgModule } from '@angular/core';
2import { Routes, RouterModule } from '@angular/router';
3import { SecondComponent } from './second/second.component';
4import { FirstComponent } from './first/first.component';
5import { FirstChildComponent } from './first-child/first-child.component';
6
7const routes: Routes = [
8 {
9 path: 'first',
10 component: FirstComponent,
11 children: [
12 {
13 path: 'first-child',
14 component: FirstChildComponent
15 }
16 ]
17 },
18 {
19 path: 'second',
20 component: SecondComponent
21 },
22 {
23 path: "",
24 redirectTo: '/first',
25 pathMatch: 'full'
26 }
27];
28
29@NgModule({
30 imports: [RouterModule.forRoot(routes)],
31 exports: [RouterModule]
32})
33export class AppRoutingModule { }
In the routes file, we have created two main routes, /first
and /second
, and a default route that will redirect to /first
. Mainly, it is for redirecting the user to the home page initially. We have also created children routing to explain the dynamic navigation for the parent-child relationship.
It's time to write the chunks of code for navigating between the component using routes.
I hope you have enjoyed this guide. There are multiple situations in which you might need dynamic routing. I have explained some of them in the above example. I tried to cover all aspects of dynamic navigation. To learn to use dynamic routing more effectively, read more about it here.