I have an Angular 4 application and my private.component.html
something like this:
<app-breadcrumb></app-breadcrumb>
<router-outlet></router-outlet>
And my routing:
const privateRoutes: Routes = [
{
path: '',
component: PrivateComponent,
children: [
{
path: 'dashboard',
component: DashboardComponent
},
{
path: 'settings',
component: SettingsComponent
},
{
path: 'companies',
component: CompaniesComponent,
children: [
{
path: 'add',
component: FormCompanyComponent
},
{
path: ':id',
component: CompanyComponent
}
]
}
]
}
];
All components on first level is rendered in router-outlet of PrivateComponent
. But I want (if possible) all other child (and I can have multiple levels), like /companies/add
or /companies/20
still rendered in the same router-outlet of my private template. My actual code, sure, expect I have the outlet inside the companies.component.html
.
This is important to implement my breadcrumb component and write "Home > Companies > Apple Inc.", for example.
It's possible create some structure like that?
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.
If parent route is having a component assigned, then that parent route’s component should have a router-outlet. If parent path is not having a component assigned, then it will move further top in hierarchy and choose the nearest router-outlet.
However, this is a post about nested routes, not just rendering normal routes. Typically with nested routes, the parent Route acts as a wrapper over the child Route. This means that both the parent and the child Route s get rendered.
You can create a named Router outlet using the name property of the <router-outlet> component: How to Create Multiple Router-Outlets? You can have multiple outlets in the same template:
Adding to @Karsten's answer, basically what you want is to have a componentless route and the empty path as the default component such as this:
const privateRoutes: Routes = [
path: 'companies',
data: {
breadcrumb: 'Companies'
}
children: [{
path: '', //url: ...companies
component: CompaniesComponent,
} {
path: 'add', //url: ...companies/add
component: FormCompanyComponent,
data: {
breadcrumb: 'Add Company' //This will be "Companies > Add Company"
}
}, {
path: ':id', //url: ...companies/5
component: CompanyComponent
data: {
breadcrumb: 'Company Details' //This will be "Companies > Company Details"
}
}
]
];
You will need to modify the breadcrumb dynamically to change "Company Details" with the actual company name.
The /companies/add
or /companies/20
routes would still be rendered inside the first router-outlet if you would make companies
a componentless route.
This means you would have to leave out the component definition for that route, it would look like this:
//...
{
path: 'companies',
children: [
{
path: 'add',
component: FormCompanyComponent
},
{
path: ':id',
component: CompanyComponent
}
]
}
Update
{
path: 'companies',
component: CompaniesComponent
},
{
path: 'companies/add',
component: FormCompanyComponent
},
{
path: 'companies/:id',
component: CompanyComponent
}
But this a little nasty in my opinion
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