将当前路由替换为其他路由

Replace the current route with other route

在 Angular 2.0 中,我们可以使用 route.navigate( [ 'routeName' ] ) 导航到其他路线。但此操作会将当前路线存储在浏览器历史记录中。
在浏览器历史记录中替换当前路由并导航到新路由的方法是什么。

你可以使用

navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>

navigateByInstruction(instruction: Instruction, _skipLocationChange?: boolean) : Promise<any>

Router 上并将 true 传递给 _skipLocationChange 以实现该效果。

(虽然我自己还没有尝试过,文档也很模糊,但我认为这就是你要找的)

提示:新的 RC.1 路由器尚不支持

你可以试试这个,它适用于 Angular RC.1 + 版本

this.router.navigate(['login'],{skipLocationChange:true});

这将导航到登录路径,而不会将登录路径存储在浏览器历史记录中。

看到这个:https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html

您必须使用选项 replaceUrl 而不是 skipLocationChange。它会将您转到新的 url,但不会保留当前页面,因此按返回不会将您送回此页面。

this.router.navigate(['login'],{replaceUrl:true});