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

前端 未结 5 901
清歌不尽
清歌不尽 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:54

    Important note for iOS

    The beforeunload event isn't supposed - presumably due to the high level of 'abuse' over the years.

    Instead you can use pagehide as recommended by Apple.

    This is part of the Page visibility API.

    https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

    So I can't seem to get pagehide to fire on desktop chrome, and I can't get beforeunload to fire on iOS Safari - so you need both - but make sure not to fire your code twice.

    @HostListener('window:beforeunload')
    @HostListener('window:pagehide')
    

提交回复
热议问题