Angular2: How to prevent/ nullify history manipulation while implementing routerCanDeactivate?

前端 未结 1 1220
轻奢々
轻奢々 2021-01-24 19:58

routerCanDeactivate() successfully prevents navigation away from component.

routerCanDeactivate(nextInstruction: ComponentInstruction, prevInstructi         


        
相关标签:
1条回答
  • 2021-01-24 20:54

    Due to window.onpopstate triggering at every back button click,

    I thought that a pushState will be required. But that wasn't the case, and following code works perfectly.

    routerCanDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) {
        if(confirm("Do you want to discard unsaved changes ?")){
          return true;
        }
        else {
          history.forward();
          return false;
        }
      }
    

    But it would be nice if somebody could explain what is happening because I don't have much knowledge of history api.

    0 讨论(0)
提交回复
热议问题