Shekhar
Shekhar

Reputation: 623

How to clear navigation history in Angular router

I have a use case, in which I need to clear the navigation history using angular-router (so that hardware back button press does not 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 in-built angular functionality I can use?

Upvotes: 5

Views: 19959

Answers (3)

winsten
winsten

Reputation: 1

import { Location } from '@angular/common'; URL is your '/tabs/home' const URL = '/tabs/home' this.location.replaceState(URL)

Upvotes: 0

Shekhar
Shekhar

Reputation: 623

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

Upvotes: 2

trisek
trisek

Reputation: 789

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});

Upvotes: 12

Related Questions