Search code examples
angularangular2-routingbrowser-history

Circle: window.history.back() and location.back()


In my Angular app, I have a view and a edit view with a input form. The problem is:

If I use the browser's back button, window.history.back() or the equivalent location.back() in Angular2 once, it will work fine as expected.

But if I trigger successive (two times) the browser's back button, window.history.back() or the equivalent location.back(), it will start a circle - meaning returning to the initial site.

Another strange behavior with the same sites is:

  1. From the view I go to edit view with normal this.router.navigate(['/edit']);
  2. location.back() to go back to view
  3. I go again to the edit view with normal this.router.navigate(['/edit']);
  4. Now I have to trigger three times location.back() to go back to view.

The history seems to be corrupted.

Does anybody have a idea what the problem could be. Is it possible that it correlate with the form?


Solution

  • Do not import routing multiple times.

    export const routing: ModuleWithProviders = RouterModule.forRoot(routes)