Angular 2+连续路线参数;最后一个无组件

问题描述 投票:2回答:1

这里我有一个路由路径,后跟两个路由参数,路由看起来像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',
          },
        ]
      },
    ]
  },
];

我得到了拒绝承诺,说“必须提供以下其中一项:component,redirectTo,children或loadChildren”。

所以我将redirectTo: ':dashboardId'添加到:dashboardParam子路径,我得到“无法重定向到':dashboardId'。找不到':dashboardId'。”

如何在不收到错误的情况下添加第三个参数?

angular angular4-router
1个回答
2
投票

实际上,如果没有相应的组件去或重定向,你就无法编写路由。你可以写这样的

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) {
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.