【发布时间】:2019-05-29 20:34:28
【问题描述】:
使用 Angular 路由器,我想导航到另一个 url 而不将其添加到浏览器历史记录中。
this.router.navigateByUrl(`${pathWithoutFragment}#${newFragment}`);
我可以只使用 Angular 吗?
【问题讨论】:
标签: javascript angular router history
使用 Angular 路由器,我想导航到另一个 url 而不将其添加到浏览器历史记录中。
this.router.navigateByUrl(`${pathWithoutFragment}#${newFragment}`);
我可以只使用 Angular 吗?
【问题讨论】:
标签: javascript angular router history
正如@IngoBürk 在评论中提到的,您可以在不使用skiplocationChange 的情况下获得相同的结果
this.router.replaceUrl('path')
skipLocationChange
在不将新状态推入历史的情况下进行导航。
this.router.navigateByUrl([`${pathWithoutFragment}#${newFragment}`], { skipLocationChange: true });
参考:https://angular.io/api/router/NavigationExtras#skipLocationChange
【讨论】:
replaceUrl 而不是skipLocationChange。也是NavigationExtras的一个选项。
感谢NavigationExtras,您现在可以在navigate() 中使用replaceUrl: boolean
这将替换当前的url,新的url会在地址栏中更新:
this.router.navigate([`/somewhere`], { replaceUrl: true });
这样当你按返回时它就不会记住历史中的上一页了。
【讨论】: