Search code examples
angularangular2-routingangular2-routerangular2-resolve

Angular: Differentiate between a parameter and child route


I have a situation where, I want a resolver if the child route is a param, and not if it's a path segment. Below is my code.

{
    path: 'agreement',
    children: [
      {
        path: ':id',
        component: AgreementComponent,
        resolve: { agreementDetails: AgreementDetailsResolveService }
      },
      {
        path: 'create',
        component: AgreementComponent
      }
    ]
  }

When I hit the path agreement/create, it's throwing error, as create is considered as the value of the param id and it's invalid.

Please help me with this.


Solution

  • Rearrange your route definition:

    {
        path: 'agreement',
        children: [
          {
            path: 'create',
            component: AgreementComponent
          },
          {
            path: ':id',
            component: AgreementComponent,
            resolve: { agreementDetails: AgreementDetailsResolveService }
          }
        ]
      }