Angular 7 Router - Navigate without adding to the history

Viewed 20716

Using Angular Router, I want to navigate to another url without adding it to the browser history.

this.router.navigateByUrl(`${pathWithoutFragment}#${newFragment}`);

Can I do it with only Angular?

2 Answers

Thanks to NavigationExtras you can now use replaceUrl: boolean inside navigate()

This will replace the current url, and the new URL will update in the address bar:

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

That way when you press back it will not remember the previous page in the history.

As @IngoBürk Mentioned in the comment you can achieve the same result without using skiplocationChange

 this.router.replaceUrl('path')

skipLocationChange

Navigates without pushing a new state into history.

this.router.navigateByUrl([`${pathWithoutFragment}#${newFragment}`], { skipLocationChange: true });

Ref:https://angular.io/api/router/NavigationExtras#skipLocationChange

Related