首页 文章

角度2/4连续路线参数;最后一个无组件

提问于
浏览
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 子路由,我得到"Cannot redirect to ':dashboardId'. Cannot find ':dashboardId'."

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

1 回答

  • 1

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

    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) {
            ...
        }
    }
    

相关问题