0

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 this.route.params 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.

Thanks!

1 Answer 1

1

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

this.myArray.push(p);

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! that was what I needed to get past the hump! I did use one more bit that I want to add here so that anyone else who is new at this can get it all working. I added an array to my trypescript as an array of string and used the syntax : this.trackingNumbers = Object.values(p); to make the object{} into an array of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.