Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load nested routes in same router-outlet

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?

like image 960
Paulo Rodrigues Avatar asked Jul 19 '17 18:07

Paulo Rodrigues


People also ask

What are nested routes in react router?

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.

How to choose the nearest router outlet to the parent route?

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.

What is a nested route?

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.

How to create a named ROUTER outlet?

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:


2 Answers

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.

like image 71
Sal Avatar answered Nov 03 '22 09:11

Sal


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

like image 27
Karsten Avatar answered Nov 03 '22 10:11

Karsten