routerCanDeactivate()
成功阻止导航离开组件。
routerCanDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction)
{
return confirm("Do you want to discard unsaved changes ?");
}
但如果导航是由浏览器引起的back
按钮或任何其他history.back()
扳机,
那么它不会保持历史完整并触发popState
每次触发并最终导航到浏览器的主页(在 Chrome 上测试)。
EDIT:
我将当前组件的 URL 保存在变量中currentLocation
and
试图操纵history
with history api
but
只是无法弄清楚保持其完整性所需的 api 调用的正确顺序。
routerCanDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) {
history.replaceState(null,null, this.currentLocation);
history.forward();
return confirm("Do you want to discard unsaved changes ?");
}
Due to window.onpopstate
每次点击后退按钮时触发,
我以为一个pushState
将被要求。但事实并非如此,下面的代码可以完美地工作。
routerCanDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) {
if(confirm("Do you want to discard unsaved changes ?")){
return true;
}
else {
history.forward();
return false;
}
}
但如果有人能解释发生了什么,那就太好了,因为我对此了解不多history api
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)