Preventing router from navigating after hash changing

强颜欢笑 提交于 2020-01-05 20:30:05

问题


I need to prevent router to be navigated to another page (which is done by changing the hash) if some changes are made. Tried with HashChanger but it just fires 'hashChange' events with no way to prevent it from bubbling. The answer can be inside of JS-Signals library but it's unavailabale directly for user-created SAP components.


回答1:


There is a stop function on the router https://sapui5.hana.ondemand.com/#/api/sap.ui.core.routing.Router/methods/stop

if you call it, the router will stop listening to hashchanges.

There is also function isStopped(). To (re-)activate the router, call initialize(...).




回答2:


Instead of stopping the router entirely, navigation can be prevented by event.preventDefault() within the navigate event handler.

<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.");
  }
}

Demo: https://embed.plnkr.co/wp6yes



来源:https://stackoverflow.com/questions/29165700/preventing-router-from-navigating-after-hash-changing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!