1

I am redirecting to my Angular2 site with some (optional) appended parameters like so:

https://my-site.com/?param1=some-value1&param2=some-value2

So i want to extract the values into variables if they exist:

let value1 = extractValueByParam(url, param1Str);

Can it be done with router variables? Or what is the "correct" way of doing this?

So far the best method I have found is to use window.location.href value and parse it myself, but it does not seem like the right thing to do.

Thanks!

2 Answers 2

1

You should use queryParams:

constructor (private _ar: ActivatedRoute) {}

ngOnInit() {
   this._ar.params.subscribe(params => console.log(params)); // params used with '/'
   this._ar.queryParams.subscribe(params => console.log(params)); // optional params used with '?'/'&'
}

official docs:

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

4 Comments

Exactly what I was looking for! Thanks!
One more thing, I see that the percentage signs (%) are being replaced with '/' in queryParams , how do I keep these percentage signs as i need to carry them forward for the BE
which percentage signs? don't know the internal logic, maybe its a bug within angular..
Just so this does not confuse anyone, I was wrong by looking at string value vs UUID value.
0

In your ngModules set the rout as:

{path: '/:param1/:param2', component: YourComponent}

Then in your component, get the value with the following:

 if(params['param1']){
    var param1 = params['param1'];
 }

2 Comments

But my url is not of this form, how would this match? to be specific, I do not have '/' between params.
Also getting this error: Unhandled Promise rejection: Invalid configuration of route '/:token': path cannot start with a slash ;

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.