Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'}

like image 897
prupp81 Avatar asked Jan 20 '16 19:01

prupp81


1 Answers

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 :)

like image 98
Ankit Singh Avatar answered Oct 16 '22 10:10

Ankit Singh