0

I have this very simple router config:

export const routes: Routes = [
  {
    path: '',
    component: MiddleComponent,
    pathMatch: 'full',
    children: [
      {
        path: '',
        redirectTo: './task-queue',
        pathMatch: 'full',
      },
      {
        path: 'task-queue',
        component: TestComponent,
      },
    ]
  },
];

Demo: https://stackblitz.com/edit/angular-a7sxdf?file=app%2Fapp.routing.ts

Where MiddleComponent's template is just <router-outlet></router-outlet>.

I'd expect that after page load I should be redirected to task-queue but I'm not. I can see the MiddleComponent is rendered but TestComponent isn't.

martin
  • 93,354
  • 25
  • 191
  • 226

2 Answers2

2

Need to add another path with redirect property for test-queue.

export const routes: Routes = [
  { path: '', redirectTo: '/task-queue' ,
    pathMatch: 'full'},
  { path: '', component: MiddleComponent,
    children: [
      { path: '', redirectTo: '/task-queue',pathMatch: 'full' },
      { path: 'task-queue', component: TestComponent }
    ] 
  }
];

Demo

Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
2

You can try this changing pathMatch to prefix for the parent route and removing the ./ in the child route

(Angular documentation for redirecting)

export const routes: Routes = [
  {
    path: '',
    component: MiddleComponent,
    pathMatch: 'prefix',
    children: [
      {
        path: '',
        redirectTo: 'task-queue',
        pathMatch: 'full',
      },
      {
        path: 'task-queue',
        component: TestComponent,
      },
    ]
  },
];

I got that solution from reading https://github.com/angular/angular/issues/14692 (this does not seem to only apply to lazy routes)

Modified stackblitz

But the solution if redirecting straight away to /task-queue from the parent works too

David
  • 33,444
  • 11
  • 80
  • 118