Encountering an issue with routing to a route containing query params. Here is the function in question:
goToLink(link) {
this.router.navigate([`${link.split('?')[0]}`, { queryParams: this.sortParams(link)}]);
}
Additionally, there is another function involved:
sortParams(link) {
let queryParams = url.split('?')[1];
let params = queryParams.split('&');
let pair = null;
let data = {};
params.forEach((d) => {
pair = d.split('=');
data[`${pair[0]}`] = pair[1];
});
return data;
}
The goToLink()
function processes URLs with query parameters, extracting and storing them into an object using the sortParams()
function. When provided with a URL like this:
https://website.com/page?id=37&username=jimmy
An object is returned that looks like this:
{id: 37, username: 'jimmy'}
This object is then passed as the query parameter when using this.router.navigate()
. However, upon routing, the query params appear as:
/page;queryParams=%5Bobject%20Object%5D
Is there something I am missing or doing incorrectly in my implementation?
Appreciate any assistance!
EDIT
Upon changing the function to:
this.router.navigate([`${link.split('?')[0]}`, { queryParams: {id: 37, username: 'jimmy'}}]);
The same URL result is observed:
/page;queryParams=%5Bobject%20Object%5D