0

How we can handle route in Angular using browser back button ,

on pressing back button I want browser should not route back page

for example ..

**

  • we go page A to B
  • then we can't go back to page A
  • on pressing browser Back button

**

I tried with Activateroute in Angular , bit this is giving a alert box on pressing back button , I dont want this , I just want to skip the page on pressing back button .

I also tried with pushState and popState but this process catch history still catching route history , I don't want that my rote should exist in browser history

Testing Anurag
  • 603
  • 4
  • 13
  • 26
  • So which route do you want to navigate? or you want to stay back in the same route? – Amit Chigadani Mar 04 '19 at 06:22
  • this might be helpful: https://stackoverflow.com/questions/51427689/angular-5-remove-route-history/51429799#51429799 – alt255 Mar 04 '19 at 06:27
  • @AmitChigadani no I dont want to go to same route instead of that I want to go the home page – Testing Anurag Mar 04 '19 at 06:30
  • In that case you may have to implement [CanDeactivate](https://angular.io/api/router/CanDeactivate) Guard against the current route and then navigate to `/home` from there. – Amit Chigadani Mar 04 '19 at 06:32

2 Answers2

0

You can use onPopState method of handling back event something like this.

location.onPopState(() => {

    console.log('pressed back!');

});

This stackoverflow question might help you.

ZearaeZ
  • 899
  • 8
  • 20
0

These are two different ways for handling your use case.

1.

history.pushState(null, null, document.URL);

2.

window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});
Eeshwar Ankathi
  • 260
  • 3
  • 11