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

前端 未结 5 899
清歌不尽
清歌不尽 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 14:58

    This worked for me. Defined in the page component constructor

    window.addEventListener("beforeunload", (event) => {
       event.preventDefault();
       event.returnValue = "Unsaved modifications";
       return event;
    });
    

    Define the returnValue only if you want to prompt user before unload.

    Work only if the user interract with the page (e.g. click).

提交回复
热议问题