Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested routing with nested modules

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.

like image 733
Rahul Patil Avatar asked Dec 27 '16 16:12

Rahul Patil


People also ask

Can we have multiple routing modules in Angular?

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.

Can we have nested modules in Angular?

Angular 2 Modules Nesting modulesModules can be nested by using the imports parameter of @NgModule decorator.

What is nested routing?

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.

What is nested routing in Angular?

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.


1 Answers

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!!

like image 150
Madhu Ranjan Avatar answered Sep 20 '22 07:09

Madhu Ranjan