figuerres figuerres - 2 months ago 13
TypeScript Question

Angular2 and Typescript need to get the router params to an array of strings

I have an angular 2 component that calls the router to navigate to a new component. it passes a set of optional parameters and the new component needs to use them. the parameters are a set of package tracking numbers that do not have a set number and do not have different names.
I have

which is an Observable<> but what I need is an array of string.
here is what starts the process:

private trackingNumbers: string[];
this.router.navigate(['/tracklist' , this.trackingNumbers ]);

so the url looks like: localhost:3000/tracklist;0=1111;1=2222;2=333
so in the tracklist component I want to find out how many tracking numbers were passed and what they are.
I want to turn the observable params to an array of strings like it was before the navigate call.



you can use ActivatedRoute from angular 2 router in your tracklist component:

import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute) {
    this.route.params.subscribe(p => {

console.log should look like this:

Object { 0: 1111, 1: 2222, 2:333 }

so all you have to do is to take the p and push it into an array or something like this.

Instead of console.log(p):


And now the tracklist component should have all the params stored in an array.