prupp81
prupp81

Reputation: 161

Angular2 Routing redirect with routeParams

Is there a way to access the routerParams in the redirecTo-Statement?

I want to pass the orderId of the 'Order' route to the 'OrderDashboard' route but I can't figure out what to write instead of ???

If I replace ??? with 3 all works fine (in case the user only is intrested in order number 3 ;-) )

{path: '/:orderId', name: 'Order', redirectTo: ['OrderDashboard', {orderId:???}]} {path: '/:orderId/dashboard', name: 'OrderDashboard'}

Upvotes: 11

Views: 5796

Answers (1)

Ankit Singh
Ankit Singh

Reputation: 24945

Late to party but anyway,

I couldn't find any reference for accesing the routeParams at the time of router.config,
But still you can achieve the same behaviour by using one of these techniques


1. Defining two routes using Same Component

(not exactly same as url will be different in address bar)

{path: '/:orderId', name: 'Order', component: OrderDashboard}
{path: '/:orderId/dashboard', name: 'OrderDashboard', component: OrderDashboard}

2. Using a disposable component as proxy

{path: '/:orderId', name: 'Order', component: OnlyToRedirect}
{path: '/:orderId/dashboard', name: 'OrderDashboard', component: OrderDashboard}

export class OnlyToRedirect{
  constructor(routeParams: RouteParams, router: Router){
    router.navigate(['OrderDashboard', {orderId: routeParams.params}])
  }
}

Hope this works :)

Upvotes: 6

Related Questions