Search code examples
angularangular2-router3

"child" Route path should include parent route path


Thats my route config:

export const SchoolyearsRoutes: RouterConfig = [
  { path:'', terminal:true, redirectTo: '/schoolyears'},
  { path: 'schoolyears',  component: SchoolyearsListComponent },
  { path: 'schoolyears/edit/:id', component: SchoolyearsEditComponent },
  { path: 'schoolyears/create', component: SchoolyearsCreateComponent }

but I want to have it as:

export const SchoolyearsRoutes: RouterConfig = [
  { path:'', terminal:true, redirectTo: '/schoolyears'},
  { path: 'schoolyears',  component: SchoolyearsListComponent },
  { path: 'edit/:id', component: SchoolyearsEditComponent },
  { path: 'create', component: SchoolyearsCreateComponent }

I want that edit/create route prepend the base url 'schoolyears' implicitly during runtime.

How can I achieve this without restructure my components?

UPDATE

Although I use TypeScript I get a runtime exception which should actually happen at compile time:

browser_adapter.ts:84EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'annotations' of undefined

Solution

  • export const SchoolyearsRoutes: RouterConfig = [
      { path:'', terminal:true, redirectTo: '/schoolyears'},
      { path: 'schoolyears', component: SchoolyearsRootComponent, children: [
         { path: '',  component: SchoolyearsListComponent },
         { path: 'edit/:id', component: SchoolyearsEditComponent },
         { path: 'create', component: SchoolyearsCreateComponent }
      ] }
    ]
    

    Isn't it something that resolves your case?