代码之家  ›  专栏  ›  技术社区  ›  Guilherme Caixeta

关闭浏览器时保存表单-Angular 5[复制]

  •  0
  • Guilherme Caixeta  · 技术社区  · 8 年前

    我在@HostListener上设置了一个断点,当我打开另一个组件时,它不会在那里中断。

    enter image description here

    我的代码中有遗漏吗?

    import { Component, OnInit, HostListener } from '@angular/core';
    
    Component({
        templateUrl: 'new-component.html'    
    })
    
    export class NewComponent implements OnInit {
        @HostListener('window:beforeunload') onBeforeUnload() {
                PostCall();
        }
    }
    
    0 回复  |  直到 8 年前
        1
  •  9
  •   Grégory Elhaimer    8 年前

    window:beforeunload 是在实际卸载页面之前触发的浏览器事件。

    当您导航到另一个组件时,您实际上并没有卸载该页面。

    ngOnDestroy 当组件被销毁时调用。实现以下接口:

    https://angular.io/api/core/OnDestroy

    export class NewComponent implements OnDestroy{
        ngOnDestroy() {
                PostCall();
        }
    }
    
        2
  •  17
  •   Chandru    8 年前

    @HostListener('window:unload', ['$event'])
    unloadHandler(event) {
        this.PostCall();
    }
    
    @HostListener('window:beforeunload', ['$event'])
    beforeUnloadHander(event) {
        return false;
    }
    
    PostCall() {
        console.log('PostCall');
    }
    
        3
  •  -1
  •   Patrik Kelemen    6 年前

    只需使用 window:beforeunload

    Return false将发出警报:)

    @HostListener('window:beforeunload')
      onBeforeUnload() {
        return false;
    }