45

At the start of application i want to load child route.

Right now URLcome but respective component not load on that section but when again hit the actual URL it comes.

like route configure is

  const appRoutes: Routes = [
        { path: 'a', component: AComponent,
         children:[
                   { path:'x',component:AXcomponent }
                   ]
        },
        { path: 'b', component: bComponent, }
]

Now i want to load path a/x how i will load at the start of page ?

AArias
  • 2,558
  • 3
  • 26
  • 36
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
  • Maybe I didn't understand correctly, but you should be able to use `{ path: '', redirectTo: '/a/b', pathMatch: 'full' },` so that your empty path ' ' redirects to '/a/b' and then loads the corresponding component. – Alex Beugnet Jan 18 '17 at 10:55
  • it not working url is set but first time component not loaded but when hit direct url then its loaded – Rituraj ratan Jan 19 '17 at 05:47

2 Answers2

116

Add empty path routes as redirect automatically

const appRoutes: Routes = [
    {
        path:'',
        redirectTo: 'a',
        pathMatch: 'full' 
    },
    {
        path: 'a',
        component: AComponent,
        children:[
            {
                path:'',
                redirectTo: 'x',
                pathMatch: 'full' 
            },
            {
                path:'x',
                component: AXcomponent 
            }
        ]
    },
    { 
        path: 'b',
        component: bComponent
    }
];
Santanu Biswas
  • 4,699
  • 2
  • 22
  • 21
6

The accepted answer is no good if your child route contains an outlet.

For example I am displaying an 'order' by ID and using a named outlet 'detail' for the summary as well as other panels.

/orders/5000001/(detail:summary)
/orders/5000001/(detail:edit)

Because these URLS are so ugly I also wanted the following two to redirect:

/orders/5000001           =>    /orders/5000001/(detail:summary)
/orders/5000001/summary   =>    /orders/5000001/(detail:summary)

So I tried a componentless redirect in the child:

{ path: '', pathMatch: 'full', redirectTo: '(detail:summary)' }

This fails because a 'redirectTo' needs to be absolute (I'm sure there's a complex technical reason for this). So then I tried this:

{ path: '', pathMatch: 'full', redirectTo: '/order/:id/(detail:summary)' }

And that fails because :id is not a named variable in the context of the child.

So in the end I figured this out:

 children: [

     { path: ':id', pathMatch: 'full', redirectTo: '/orders/:id/(detail:summary)' },
     { path: ':id/summary', pathMatch: 'full', redirectTo: '/orders/:id/(detail:summary)' },
     {
         // this goes in the main router outlet
         path: ':id', component: OrderFulldetailsComponent,

         children: [

             { path: 'summary', component: OrderSummaryComponent, outlet: 'detail' },
             { path: 'edit', component: OrderEditComponent, outlet: 'detail' }
         ]
     }
 ]

Note it is important to do pathMatch: 'full' on path: ':id' or it will match when you hit /:id/(detail:summary) and go nowhere.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689