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

标签 angular angular4-router

这里我有一个路由路径,后面跟着两个路由参数,路由看起来像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/

相关文章:

angular - 使用异步/等待时,错误处理程序返回默认值

Angular 2 Dart : How to detect click on everything but element?

javascript - 在输入模糊上保存数组元素

angular - 在 Angular 4 延迟加载中用模块名称替换 block 名称

javascript - 底部的箭头表示 Angular 事件链接

angular - 如何在同一组件上刷新路由更改的组件?

angular - 如何在 Angular 中使用 Azure Maps

angular - 在 Angular 中收听第三方库广播的事件

angular - 如何以 Angular 4 路由

angular - 使用相同的 URL 参数重新加载 Angular 4+ 路由