Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query params not working properly

In my Angular2 app I need to pass some optional arguments to a route. Because they are optional, I decided to use queryParams.

I am using the following code to pass the argument:

public recoverPassword() {
    this.router.navigate(
        ['/access/recover-password', 
        { queryParams: { 'email': this.model.email }} ]
    );
}

I get this URL:

http://localhost:4200/access/recover-password;queryParams=%5Bobject%20Object%5D

As you can see, the parameter is completely wrong. And I can't parse it the in target component. The correct url should be:

http://localhost:4200/access/recover-password;[email protected]

I followed the official doc, but I can't find a working solution.

like image 870
user3471528 Avatar asked Apr 02 '26 21:04

user3471528


1 Answers

queryParams should be passed as a second argument (to be more precise, it is just a part of optional second argument NavigationExtras, you can read more about it here), not as a part of first argument which is a route you want to navigate to. This should do the trick:

public recoverPassword() {
    this.router.navigate(
        ['/access/recover-password'], 
        { queryParams: { 'email': this.model.email } }
    );
}
like image 134
Stefan Svrkota Avatar answered Apr 08 '26 15:04

Stefan Svrkota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!