Is there any lifecycle hook like [removed] in Angular2?

前端 未结 5 894
清歌不尽
清歌不尽 2020-11-27 14:19

Is there any lifecycle hook like window.onbeforeunload in Angular2? I already googled and searched on stackoverflow, but found nothing

5条回答
  •  北海茫月
    2020-11-27 15:11

    or

    @Component({ 
      selector: 'xxx',
      host: {'window:beforeunload':'doSomething'}
      ..
    )}
    

    or

    @Component({ 
      selector: 'xxx',
      ..
    )}
    class MyComponent {
      @HostListener('window:beforeunload')
      doSomething() {
      }
    }
    

    This is how to listen to global events. I don't know if the special behavior of this event is supported where the return value is used as text for the conformation dialog.

    You can still use

    export class AppComponent {  
      constructor() {
        window.onbeforeunload = function(e) {
          return 'Dialog text here.';
        };
      }
    }
    

    Like explained here https://developer.mozilla.org/de/docs/Web/API/WindowEventHandlers/onbeforeunload

提交回复
热议问题