Search code examples
javascriptangularangular5angular-routingurl-parsing

Angular return URL is wrong


In my Angular 5 Application I am doing something like this at

this.returnUrl = this.route.snapshot.queryParams['returnUrl']

I am using this when a user accesses a route but it's not logged in. He is redirected to the login page and after login he should be going back to this return url.

The problem is that http://localhost:4200/schedule?selectedDate=2018-01-29 when accessing this route, the return url becomes:

http://localhost:4200/login?returnUrl=%2Fschedule%3FselectedDate%3D2018-01-29 and after the successful login, the application tries to go to the http://localhost:4200/%2Fschedule%3FselectedDate%3D2018-01-29 URL but that throws a 404 error since it does not recognize the path.

Any idea how can I stop Angular changing my url to this format ? I was assuming that it would pick up the correct URL.


Solution

  • I managed to somehow fix this by instead of using

    this.router.navigate(this.returnUrl) 
    

    I used

    this.router.navigateByUrl(this.returnUrl);