Angular 2 - Why my all routes are not redirected?

236 views Asked by At
[{
    path: 'menu',
    component: MenuComponent
  },
  {
    path: 'how',
    component: HowItWorksComponent
  },
  {
    path: '',
    pathMatch: 'prefix',
    redirectTo: 'menu'
  }]

Above is my root route config why my all routes are not redirected to the menu because empty string should be a prefix to all routes. It shows respective components on /how and /menu and perfectly redirects to menu on / but it should always redirect to /menu shouldn't it?

2

There are 2 answers

2
Oluwafemi Sule On

You have to set useAsDefault on /menu.

[{
   path: 'menu',
   component: MenuComponent,
   useAsDefault: true,
},
...
0
John On

If you want your last path to catch anything that the first two don't, then you'll want:

{
  path: '**',
  redirectTo: 'menu'
}

As explained in the angular docs, ** is a special wildcard route