I am working on an Angular 2 app for the first time.
I have routing similar to this -
/home
/projects/
/projects/:id/members
/projects/:id/members/:id/tasks
From all the references, tutorials and articles I could find on the internet. I only found approach similar to this -
[
{
path: "home", component: HomeComponent
},
{
path: "", redirectTo: "home", pathMatch: "full"
},
{
path: 'projects',
component: ProjectComponent,
children: [
{
path: ':id',
component: ProjectDetailsComponent,
children: [
{
path: 'members',
component: MemberComponent,
children : [
{
path: ':id',
component : MemberDetailsComponent,
children : [
{
path: 'tasks',
component : TasksComponent
}
]
}
]
},
]
}
]
}
]
This works well. However, I feel, this is sort of strictly typed approach and can create when there are a lot of components in place.
I have created feature modules called ProjectModule, MemberModule, TaskModule. There will be several more modules under ProjectModule too..
What is the best way to have nested routes in place in such scenario? The lazy loading sort of works but url appears like http://localhost/members/
instead of http://localhost/projects/12/members
even though members is under loadChildren
.
Create an Angular 8 application which will have Nested Routing up to three levels. Each level of components has its own Router Module. We will also discuss How to use lazy loading concept in Angular 8 Routing to load components on run time when they are called.
Angular 2 Modules Nesting modulesModules can be nested by using the imports parameter of @NgModule decorator.
To recap, nested routes allow you to, at the route level, have a parent component control the rendering of a child component. Twitter's /messages route is the perfect example of this. With React Router, you have two options for creating nested routes.
Nested routes are routes within other routes. In this tutorial, we will show you how to create a child route and display the child components. The Angular allows us to nest child routes under another child routes effectively creating a Tree of routes.
try below,
Check this Plunker
App Routes
const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{
path: 'projects',
loadChildren: 'app/project.module#ProjectModule'
}
];
Project Module Routes
const projectRoutes: Routes = [
{
path: '',
component: ProjectComponent ,
children: [
{
path: ':id',
component: ProjectDetailsComponent,
children:[
{
path: 'members',
loadChildren: 'app/member.module#MemberModule'
}
]
}
]
}
];
Member Module Routes
const memberRoutes: Routes = [
{
path: '',
component: MemberComponent,
children: [
{
path: ':id',
component: MemberDetailsComponent
}
]
}
];
Hope this helps!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With