Preventing router from navigating

后端 未结 2 1499
慢半拍i
慢半拍i 2021-01-25 07:07

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\' e

2条回答
  •  自闭症患者
    2021-01-25 07:13

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

    
    
    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

提交回复
热议问题