使用Angular Router,我想导航到另一个URL而不将其添加到浏览器历史记录中。
this.router.navigateByUrl(`${pathWithoutFragment}#${newFragment}`);
仅使用Angular,我能做到吗?
使用Angular Router,我想导航到另一个URL而不将其添加到浏览器历史记录中。
this.router.navigateByUrl(`${pathWithoutFragment}#${newFragment}`);
仅使用Angular,我能做到吗?
感谢NavigationExtras,现在您可以在navigate()
内使用replaceUrl: boolean
这将替换当前URL,并且新的URL会更新到地址栏中:
this.router.navigate([`/somewhere`], { replaceUrl: true });
这样当您按下“返回”时,它将不会记住历史记录中的上一页。
如@IngoBürk在评论中提到的,您可以在不使用skiplocationChange的情况下实现相同的结果
this.router.replaceUrl('path')
skipLocationChange
在不向历史记录中添加新状态的情况下导航。
this.router.navigateByUrl([`${pathWithoutFragment}#${newFragment}`], { skipLocationChange: true });
Ref:https://angular.io/api/router/NavigationExtras#skipLocationChange
replaceUrl
而不是skipLocationChange
。这也是NavigationExtras
的一个选项。 - Ingo Bürk