Using angular2, I want to open an auxiliary route programatically in my component.
This code opens the route with the 'list' path and it opens the route in the correct named router outlet, but I am not sure how to pass parameters to the route:
this.router.navigate(['./', { outlets: { 'list-outlet': 'list' } }]);
In the end, I just needed to use an array to pass my params... See
param1
andparam2
below.Note... I had to change the path in my routing configuration:
And then in my
ngOnInit
function, I can pull the params out of the router as Muirik shows.The route for the component that displays the details for a specific product would need a route parameter for the
ID
of that product. We could implement this using the following Routes:Note
:id
in the path of theproduct-details
route, which places the parameter in the path. For example, to see theproduct-details
page for product withID
5, you must use the following URL:localhost:3000/product-details/5
Linking to Routes with ParametersIn the
ProductList
component you could display a list of products. Each product would have a link to theproduct-details
route, passing theID
of the product:Note that the
routerLink
directive passes an array which specifies the path and the route parameter. Alternatively we could navigate to the route programmatically:The
ProductDetails
component must read the parameter, then load the product based on theID
given in the parameter. The ActivatedRoute service provides aparams
Observable which we can subscribe to to get the route parameters (see Observables)The reason that the
params
property on ActivatedRoute is an Observable is that the router may not recreate the component when navigating to the same component. In this case the parameter may change without the component being recreated.Plunkr example
Information from here