Angular 7 路由器 - 导航而不添加到历史记录

Angular 7 Router - Navigate without adding to the history

使用 Angular 路由器,我想导航到另一个 url 而不将其添加到浏览器历史记录。

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

我可以只使用 Angular 吗?

正如@IngoBürk 在评论中提到的,您可以在不使用 skiplocationChange 的情况下获得相同的结果

 this.router.replaceUrl('path')

skipLocationChange

Navigates without pushing a new state into history.

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

参考:https://angular.io/api/router/NavigationExtras#skipLocationChange

感谢 NavigationExtras,您现在可以在 navigate()

中使用 replaceUrl: boolean

这将替换当前的 url,新的 URL 将在地址栏中更新:

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

这样当你按后退它就不会记住历史中的上一页。