How to clear navigation history in Angular router

Viewed 6052

I have a use case in which I need to clear the navigation history using angular router (so that hardware back button press does nnot take me to the previous page but homepage). I have been looking around for a straightforward solution for this but haven't found any. I have suggestions of using nativescript but I don't want to increase my bundle-size for this requirement. Please advise if there is any inbuilt angular functionality I can use?

2 Answers

You can do it in two ways. If you don't need a state in the history then you can use something like this:

this.router.navigate([`/myPage/`], { relativeTo: this.route, skipLocationChange: true });

Or if you just want to replace current state in a history with a new one:

this.router.navigate(['/home'], {replaceUrl: true});

The only way I was able to make it work was by using NavContoller

   this.nav.navigateRoot('/tabs/home');

This removed all the pages from the stack and navigated to the homepage

Related