Angular2 中是否有像 window.onbeforeunload 这样的生命周期钩子? [英] Is there any lifecycle hook like window.onbeforeunload in Angular2?

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

问题描述

Angular2 中是否有像 window.onbeforeunload 这样的生命周期钩子?我已经在 stackoverflow 上用谷歌搜索过,但一无所获

解决方案

@Component({选择器:'xxx',主机:{'window:beforeunload':'doSomething'}..)}

@Component({选择器:'xxx',..)}类我的组件{@HostListener('window:beforeunload')做点什么() {}}

这就是监听全局事件的方法.我不知道是否支持此事件的特殊行为,其中返回值用作构造对话框的文本.

您仍然可以使用

export class AppComponent {构造函数(){window.onbeforeunload = 函数(e){return '此处的对话文本.';};}}

喜欢这里解释 https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

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>

or

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

or

@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.

You can still use

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

Like explained here https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

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

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