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
<
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");
}
}
create a file beforeunload.js
that you put in the assets
folder, containing
window.beforeunload = function() {
// Do your stuff
}
In your angualr-cli.json
, simply import the file :
"scripts": [
"assets/beforeunload.js"
],