Go to Previously Visited Route in Angular 4 App

User 5842 Source

Is there a way to "unwind" the routes that some user has gone through while on my web application? I currently have an Angular 4 app and am working on an unauthorized page.

Here's what happens:

  • User lands on unauthorized page after receiving a 403 from some network request
  • User hits an okay button on said page and is redirected back to the login screen

I was wondering if there was instead a way to go back to the previous page they were on before they visited the unauthorized page. So:

  1. Home Page
  2. Customer Page
  3. Administrator Page (unauthorized)

At this point, they click the okay button and are taken back to the Customer Page.

Do I necessarily have to interact with routes? Maybe there is another way?


If the route URL is transformed to something like https://mywebsite/unauthorized, I'd ideally want to navigate to the route that was hit before entering this state.




answered 6 months ago Kraken #1

You can use location service, after injecting it into component just use


and it will be similar as if you press back in your browser.

comments powered by Disqus