title: Angular 5 prevent or warn user before back button click
body: <p>So I have a SPA application built in angular, now my issue is when a user is on my app if they press the (browser) back button, sometimes if data was sent on the previous page it can cause errors and sometime there is state that when refreshed goes away. Now Is there a  way I can warn a user before going back or simply not allow a user to go back??</p>
  
  <p>I have tried to do this in my index.html</p>
  
  <pre><code>&lt;script&gt;
      window.onbeforeunload = function() {
         return "Message";
      };
  &lt;/script&gt;
  </code></pre>
  
  <p>but it looks like this no longer works on newer browsers, I found a similar question <a href="https://stackoverflow.com/questions/18141729/browsers-back-button-click-with-confirm-box">here</a> but its from a few years ago and I've tried a few of there solutions and none of them worked..</p>
  
  <p>what is the best way in 2018 to handle this situation??</p>
  
  <p>Thanks</p>