如果进行了一些更改,我需要阻止路由器导航到另一个页面(通过更改哈希来完成)。尝试使用HashChanger但它只是触发'hashChange'事件而无法阻止它冒泡。答案可以在JS-Signals库中,但它不能直接用于用户创建的SAP组件。
答案 0 :(得分:2)
路由器上有一个停止功能https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.core.routing.Router.html#stop
如果你打电话,路由器将停止收听散列更改。
答案 1 :(得分:0)
navigate
事件处理程序中的event.preventDefault()
可以阻止导航,而不是完全停止路由器。
<App xmlns="sap.m" navigate=".onNavigate">
onNavigate: function(event) {
if (/* pending changes? */) {
event.preventDefault();
const { isBack, isBackToPage, isBackToTop } = event.getParameters();
if (isBack || isBackToPage || isBackToTop) {
window.history.go(1);
} else {
window.history.go(-1);
}
this.informUser("There are still pending changes.");
}
}