Angular 10 nested routing & Child Routes example
Today, we will discuss with you how to configure nested routing using a Router Module and outlet. now we will create an application and add nested routing and navigation for our applications.
you can easily Add nested Routing & Child Routes to your angular application by using our example. we are creating a Simple Route application. so you can see our angular 10 nested routing & Child Routes example.
Create new angular app
We will create a new angular app using the following command.
1 2 | ng new myapp cd myapp |
when you create your application at that time Angular CLI ask you Would you like to add Angular routing? (y/N). If you enter with y for Yes. then the angular router will be automatically configured.
Create a component in angular
Now we need to create three component such as home, about, and contact. So let’s run the following command to create components.
1 | ng g c home |
1 | ng g c category |
Create Nested Routes
Now, we will create two nested routes category-create and category-detail.
1 | ng g c category/category-create |
1 | ng g c category/category-detail |
Update view file in angular
src/app/app.component.html
In this example, we will create a simple navigation menu using bootstrap. we need to routerLink for redirect the different routes and also add router-outlet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <nav class="navbar navbar-expand-lg navbar-dark bg-primary"> <a class="navbar-brand" routerLink="/">\Angular 10 Router Tutorial With Example</a> <div class="collapse navbar-collapse" id="navbarText"> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" routerLink="/">Home</a> </li> <li class="nav-item"> <a class="nav-link" routerLink="/category">Category</a> </li> </ul> </div> </nav> <div class="container"> <router-outlet></router-outlet> </div> |
src/app/home/home.component.html
1 2 3 | <h3>Welcome To Home Page</h3> <p><b>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups</b></p> <p>Lorem ipsum, orLorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</p> |
src/app/category/category.component.html
1 2 3 | <h3>Welcome To Category Page</h3> <a class="btn btn-primary" routerLink="/category/create">Create</a> <a class="btn btn-primary" routerLink="/category/detail">Detail</a> |
src/app/category/category-create/category-create.component.html
1 2 | <h3>Welcome To Create Category Page</h3> <a class="btn btn-primary" routerLink="/category">Back</a> |
src/app/category/category-detail/category-detail.component.html
1 2 | <h3>Welcome To Category Detail Page</h3> <a class="btn btn-primary" routerLink="/category">Back</a> |
Adding Routing to your Angular 10
src/app/app-routing.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { CategoryComponent } from './category/category.component'; import { CategoryCreateComponent } from './category/category-create/category-create.component'; import { CategoryDetailComponent } from './category/category-detail/category-detail.component'; const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'posts', children: [ { path: '', component: CategoryComponent }, { path: 'create', component: CategoryCreateComponent }, { path: 'detail', component: CategoryDetailComponent } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } |
Run Our Angular Application
Now we will run our Angular application using the below command.
1 | ng serve |