Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest more than 2 levels of children routes in Angular 5?

I have this sample routes in angular 5. The 3 components are imported to this module. The component mark is generated under the folder of john component and james component is generated under the folder of mark component.

I want to load james component via path that looks like this: https://my-website/john-route/mark-route/james-route so I created this routes in the module file.

  const routes: Routes = [
  {
    path: 'john-route',
    component: JohnComponent,
    children: [
      {
        path: 'mark-route',
        component: MarkComponent,
        children: [
          {
            path: 'james-route',
            component: JamesComponent
          }
        ]
      }
    ]
  }
];

But my problem is, it only loads up to mark component with this [routerLink]="['mark-route']". And on james component with this [routerLink]="['james-route']", it only shows correct path https://my-website/john-route/mark-route/james-route in the URI but doesn't load the component in the page. What is happening here, how to solve this issue or what is the best solution for this?

like image 504
Jay Marz Avatar asked Apr 06 '18 08:04

Jay Marz


People also ask

Can we have multiple routes in Angular?

Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.

Can Angular 8 use multiple router outlets?

Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.

Can we use 2 router outlet in Angular?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.

Which property is used to define child routes within a routes object?

In Angular, the router lets you add child routes using the children property inside the routing module. Here you can see that the routing module has been updated with the child route and added to the array of components so we do not need to import all of them wherever we go.


1 Answers

Your MarkComponent need also to have a router-outlet inside it.

To work with child routes, your parent component must have a markup part

<router-outlet></router-outlet>

This lets your child routes to be placed in the parent component. The same must be done for all components which have children components.

For more see Routing & Navigation in Angular

like image 99
Suren Srapyan Avatar answered Sep 17 '22 18:09

Suren Srapyan