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?
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.
Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.
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.
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.
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
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