angularangular4-router

Angular 2+ consecutive route params; the last one componentless


Here I have a route path followed by two route parameters, the route looks like section/sectionId/dashboardId:

const appRoutes: Routes = [
  {
    path: 'section/:sectionId',
    component: SectionComponent,
    children: [
      {
        path: ':dashboardId',
        component: DashboardComponent
      },
    ]
  },
];

What I need is to add a third parameter to this that is componentless. I need to use it for an ID as a parameter I can pass to one of my components. So I tried this:

const appRoutes: Routes = [
  {
    path: 'section/:sectionId',
    component: SectionComponent,
    children: [
      {
        path: ':dashboardId',
        component: DashboardComponent,
        children: [
          {
            path: ':dashboardParam',
          },
        ]
      },
    ]
  },
];

I got a promise rejection that said "One of the following must be provided: component, redirectTo, children or loadChildren".

So I add redirectTo: ':dashboardId' to the :dashboardParam child route, and I get "Cannot redirect to ':dashboardId'. Cannot find ':dashboardId'."

How can I add this third parameter without getting the errors?


Solution

  • Actually you can't write a route without the corresponding component to go or redirect. You can write like this

    const appRoutes: Routes = [
      {
        path: 'section/:sectionId',
        component: SectionComponent,
        children: [
          {
            path: ':dashboardId/:dashboardParam',
            component: DashboardComponent    
          },
        ]
      },
    ];
    

    and in the component check if dashboardParam parameter was passed to the component or not via

    constructor(params: RouteParams) {
        var dashboardParam= params.get("dashboardParam");
    
        if (dashboardParam) {
            ...
        }
    }