0

When I try to pass some parameters to my URL the angular router provides some URL schemes that I don't really like.

The URLS in this example look like this:

https://whatever.io/crisis-center;id=2;foo=foo

And I would like them to look like some 'conventional' URLs.

Something like: https://whatever.io/crisis-center?id=2&foo=foo

Is there a way to change the URL scheme for the Angular2^ router? If so, any advice or example on how to do it?

1 Answer 1

2

You can use query parameters when routing in angular

this.router.navigate(['/crisis-center'], { queryParams: { id: 2, 'foo': 'foo' } });

Your URL will look like this :

http://localhost:4200/crisis-center'?id=2&foo=foo
Sign up to request clarification or add additional context in comments.

5 Comments

This is exactly what I have been looking for. Any tip on how to navigate relatively? For instance: im at name.io/somewhere and I'd like to navigate to name.io/somewhere/deeper?id=2&foo=foo. I just can get 'root' redirects of the URL. Thank you very much! (as soon as I can accept the answer I'll do so)
Import ActivatedRouter and make its instance in constructor, then use it while navigating, this.router.navigate(['crisis-center'], { relativeTo: this.activatedRoute, queryParams: { id: 2, 'foo': 'foo' } })
How should I get this.activatedRoute? I get an error saying that the property doesn't exist in my component.
import { ActivatedRoute } from '@angular/router'
I forgot to add it into the constructor thank you again

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.