Angular - Nested Routes



This chapter will discuss, what is Nested Routes and Nested Routing in Angular, including its syntax, usage and example of using in real-time angular project.

Nested Routes in Angular

In Angular, nested routes are used to define hierarchical routes for components. This technique allows you to load a specific child component only when its parent component is loaded.

For example, if you want to load all the items within the ItemsComponent, you can use a route like /items/item-lists/, where the "item-list" is nested within the "items routes".

When the <router-outlet> directive is used in a component other than the root component, it allows you to display child routes within that component, and those routes will be considered child routes of the parent component. This concept is known as nested routing, and it can be achieved by implementing nested routes.

Syntax of Angular Nested Routes

Below is the syntax to create Nested Routes in Angular −

const routes: Routes = [
  {
    path: 'parent',
    component: ParentComponent,
    children: [
      {
        path: 'child1',
        component: Child1Component
      },
      {
        path: 'child2',
        component: Child2Component
      }
    ]
  }
];

Here,

  • path: It specifies the URL segment for the route.
  • component: It defines the component to be rendered for the route.
  • children: An array of child routes nested under the parent route.

Note: The nested URL will be: "/parent/child1" and "/parent/child2".

Example of Angular Nested Routes

Below is a example of using the Nested Routes in a Angular project −

Step 1: Define Nested Routes for your application

import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { ViewItemComponent } from './view-item/view-item.component';
import { PagenotfoundComponent } from './pagenotfound/pagenotfound.component';
import { EditItemComponent } from './edit-item/edit-item.component';

export const routes: Routes = [
    {path: 'home', component: HomeComponent},
    {path: 'about', component: AboutComponent},
    {path: 'item', children:[
        {path: 'view/:id', component: ViewItemComponent},
        {path: 'edit/:id', component: EditItemComponent},
    ]},
    {path: '**', component: PagenotfoundComponent}
];

Step 2: Add your routes to your application

<h1>Angular Routing</h1>
<a routerLink="/home">Home</a><br>
<a routerLink="/about">About</a>
Item1: <a routerLink="/item/view/1">View Item1</a><a routerLink="item/edit/1">Edit Item1</a><br>
Item2: <a routerLink="item/view/2">View Item2</a><a routerLink="item/edit/2">Edit Item2</a><br>
<router-outlet></router-outlet>

Step 2: Add the 'router-outlet' directive to your ItemComponent

<p>item works!>/p>
<router-outlet></router-outlet>

Step 3: Access 'id' parameter in ViewItemComponent and EditItemComponent

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-view-item',
  imports: [],
  templateUrl: './view-item.component.html',
  styleUrl: './view-item.component.css'
})
export class ViewItemComponent implements OnInit{
  id: any;
  constructor(private route: ActivatedRoute){}
  ngOnInit(): void {
      this.route.paramMap.subscribe(res=>{
        this.id = res.get('id');
      });
  }
}

EditItemComponent:

import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-edit-item',
  imports: [],
  templateUrl: './edit-item.component.html',
  styleUrl: './edit-item.component.css'
})
export class EditItemComponent {
  id: any;
  constructor(private route: ActivatedRoute){}
  ngOnInit(): void {
      this.route.paramMap.subscribe(res=>{
        this.id = res.get('id');
      });
  }
}

Now run the application and see the output:

Nested Routes

By observing the URL in the above GIF, you can clearly see that the View and Edit components are only loaded when the Item route is activated. In the context of Angular, the child components (View and Edit) are rendered only within the parent Item route.

Advertisements