Angular2 browser back button after redirect

I have a delegating component that does nothing but allow the object by ID from the path (for example /tournament/:id), determine the type of tournament and redirect (through router.navigate(..)) to another route (for example, /tournament/:id/league)

The problem is that the return button to the browser will return to the delegating component and show an empty template of my delegating component.

Are there other options for forwarding to another component than router.navigate, given the following:

  • Browser return button will not go to the delegating component
  • The route /tournament/:id/leagueshould be reflected in the browser URL
+4
source share
1 answer

I found a workaround, still hoping that someone would come up with something more complex, as this fragment should be placed in each component.

It is placed in the constructor and checks if the new route is part of the delegating component. If so, redirect to another route.

_location.subscribe(loc => {
  let routeOfDelegatingComponent = "....";
  if (loc.url === routeOfDelegatingComponent) {
    router.navigate(['/acustomroute']);
  }
});
+1
source

Source: https://habr.com/ru/post/1676824/


All Articles