title: How can I use @HostListener('window:beforeunload') to call a method?
body: <p>I am trying to call a post method I made myself before my component is unloaded, but it doesn't work.</p>
  
  <p>I put a breakpoint on @HostListener and it doesn't break there when I open a different component.</p>
  
  <p>I'm using Chrome to debug and I turned on Event Listener Breakpoints for unload and beforeunload, which do break when I open a different component.</p>
  
  <p><a href="https://i.stack.imgur.com/uVDJY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uVDJY.png" alt="enter image description here"></a></p>
  
  <p>Am I missing something in my code?</p>
  
  <pre><code>import { Component, OnInit, HostListener } from '@angular/core';
  
  Component({
      templateUrl: 'new-component.html'    
  })
  
  export class NewComponent implements OnInit {
      @HostListener('window:beforeunload') onBeforeUnload() {
              PostCall();
      }
  }
  </code></pre>