Load nested routes in same router-outlet

…衆ロ難τιáo~ 提交于 2019-12-06 03:55:49

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!