这里我有一个路由路径,后面跟着两个路由参数,路由看起来像section/sectionId/dashboardId
:
const appRoutes: Routes = [
{
path: 'section/:sectionId',
component: SectionComponent,
children: [
{
path: ':dashboardId',
component: DashboardComponent
},
]
},
];
我需要的是添加第三个无组件参数。我需要将它用作 ID 作为可以传递给我的组件之一的参数。所以我尝试了这个:
const appRoutes: Routes = [
{
path: 'section/:sectionId',
component: SectionComponent,
children: [
{
path: ':dashboardId',
component: DashboardComponent,
children: [
{
path: ':dashboardParam',
},
]
},
]
},
];
我收到了一个 promise 拒绝,内容是“必须提供以下其中一项:组件、redirectTo、children 或 loadChildren”。
因此,我将 redirectTo: ':dashboardId'
添加到 :dashboardParam
子路由,然后收到“无法重定向到 ':dashboardId'。找不到 ':dashboardId” '."
如何添加第三个参数而不出现错误?
最佳答案
实际上,如果没有相应的组件去或重定向,你就无法编写路由。可以这样写
const appRoutes: Routes = [
{
path: 'section/:sectionId',
component: SectionComponent,
children: [
{
path: ':dashboardId/:dashboardParam',
component: DashboardComponent
},
]
},
];
并在组件中检查 dashboardParam
参数是否通过
constructor(params: RouteParams) {
var dashboardParam= params.get("dashboardParam");
if (dashboardParam) {
...
}
}
关于Angular 2+ 连续路径参数;最后一个无组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924139/