ple*_*ock 34 typescript angular2-routing angular
如何更换历史而不是在角度2的新路由器(rc.1)中推新?
例如,我在一个问题列表中(/questions)在新路线()中打开一个新模态/questions/add,并在添加一个新问题后,我转到问题视图(/questions/1).如果我按回去,我想/questions转而去/questions/add
Jas*_*son 71
如果你使用
router.navigate
Run Code Online (Sandbox Code Playgroud)
然后
location.replaceState
Run Code Online (Sandbox Code Playgroud)
使用完全相同的路径,您可以触发通常发生的更改,以及替换历史记录.
例如,在您的情况下:
this.router.navigate(['questions/1']);
this.location.replaceState('questions/1');
Run Code Online (Sandbox Code Playgroud)
如果需要向路径添加参数,可以使用创建位置的URL
router.serializeUrl(router.createUrlTree(/* what you put in your router navigate call */));
Run Code Online (Sandbox Code Playgroud)
在你的例子中:
this.router.navigate(['questions/1', {name:"test"}]);
this.location.replaceState(this.router.serializeUrl(this.router.createUrlTree(['questions/1', {name:"test"}])));
Run Code Online (Sandbox Code Playgroud)
似乎角度路由器现在带有替换url选项.在你的例子中:
this.router.navigate(["questions/1"], {replaceUrl:true});
Run Code Online (Sandbox Code Playgroud)
目前存在一个问题,即在短时间内完成的多次导航可能无法正确替换URL.作为临时解决方法,将导航功能包装在超时中,以便在单独的循环中触发每个导航功能:
setTimeout(()=>{
this.router.navigate(["questions/1"], {replaceUrl:true});
});
Run Code Online (Sandbox Code Playgroud)
小智 5
您想使用 Locations 类中的 ReplaceState
https://angular.io/docs/ts/latest/api/common/index/Location-class.html#!#replaceState-anchor
| 归档时间: |
|
| 查看次数: |
33478 次 |
| 最近记录: |