[removed] - Importing in Angular2

前端 未结 2 1751
栀梦
栀梦 2021-01-24 01:57

I am trying to use window.onbeforeunload in my project but I do not know how to use it.

I have tried in 2 ways:

1st way

<         


        
2条回答
  •  逝去的感伤
    2021-01-24 02:32

    you have to import @HostListener and use it like below in your component

    import { Component, OnInit, HostListener } from '@angular/core';
    
    export class TestComponent implements OnInit {
        @HostListener('window:beforeunload', ['$event']) onBeforeUnload(event) {
               // do something here 
              console.log("method called");
        }
    }
    

提交回复
热议问题