I'm facing an issue while developing an application using Angular 6. While passing an object as a value of a property of queryParams: { prop: value}, built in function converts object to a string, and I get an usual in js [object Object].
What I'm trying to do: I have a query string that i use to send request to my server. I want to use it now, to update query params of current route. Example of my query string stringified with npm-qs lib:
?priceRange[min]=1&priceRange[max] = 2
Let's take a look at my object that I parse with the same lib, back to an object:
priceRange: {min: "1", max: "2"}
What queryParams converts it to:
priceRange=%5Bobject%20Object%5D
To do so, I parse it back to a proper object, and then, I use a method posted somewhere here:
constructor(
private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
) {}
updateQueryParams() {
this.router.navigate([], {
relativeTo: this.activatedRoute,
queryParams: {
priceRange: {min: "1", max: "2"} (hardcoded this to simplify example)
}
});
}
With the code above I'm trying to:
Stay at the same page (
[]along withrelativeTo: this.activatedRoute, makes me to stay at the same page as far as I understand);Change url to
.com/?priceRange[min]=1&priceRange[max]=2(or something like that)
I'm asking, is there a way to use objects as a values in queryParams, or are there any suggestions? I've read through angular.io docs, there is nothing about such case, just simple examples of param1=q & param2=w, same actually in several stack posts. Maybe I can somehow pass a query string to my url using Angular means? (Not using those queryParams of router.navigate() method).
Thanks in advance!