Angular2中是否有任何生命周期挂钩,例如window.onbeforeunload? [英] Is there any lifecycle hook like window.onbeforeunload in Angular2?

查看:99
本文介绍了Angular2中是否有任何生命周期挂钩,例如window.onbeforeunload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular2中是否有诸如window.onbeforeunload之类的生命周期挂钩? 我已经用Google搜索和搜索stackoverflow,但是什么也没找到

Is there any lifecycle hook like window.onbeforeunload in Angular2? I already googled and searched on stackoverflow, but found nothing

推荐答案

<div (window:beforeunload)="doSomething()"></div>

@Component({ 
  selector: 'xxx',
  host: {'window:beforeunload':'doSomething'}
  ..
)}

@Component({ 
  selector: 'xxx',
  ..
)}
class MyComponent {
  @HostListener('window:beforeunload')
  doSomething() {
  }
}

这是侦听全局事件的方法.我不知道在返回值用作构想对话框的文本的情况下是否支持此事件的特殊行为.

This is how to listen to global events. I don't know if the special behavior of this event is supported where the return value is used as text for the conformation dialog.

您仍然可以使用

export class AppComponent {  
  constructor() {
    window.onbeforeunload = function(e) {
      return 'Dialog text here.';
    };
  }
}

就像这里的 https://developer.mozilla.org/de /docs/Web/API/WindowEventHandlers/onbeforeunload

这篇关于Angular2中是否有任何生命周期挂钩,例如window.onbeforeunload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆