title: Execute a method when a user navigates away from a page/route: Angular2
body: <p>I'm trying to run a function whenever current route is changed. For example, if we are currently on "..../user/user-details", where user-details is a separate component. I want to execute a method whenever the route is changed (shouldn't matter if route changes from the UI or directly from the URL).
  I did something in the user-details component:</p>
  
  <pre><code>constructor(private router: Router) {
    router.events.subscribe(() =&gt; {
      console.log('Route changed');
   });
  }
  </code></pre>
  
  <p>But the issue with this approach is, it runs on every route change (whether or not we are navigating to or from user-details page). So this is subscribing to app wide routes. I just want it to run whenever we are navigating away from user-details component. Is their a way to do that?</p>