简体   繁体   English

Angular2多次调用ngOnInit(以及其他所有生命周期挂钩方法)

[英]Angular2 invokes ngOnInit (and every other lifecycle hook method) more than once

I'm experiencing a strange situation under Google Chrome (newest) - I'm navigating to a list in my ng2 driven web application, collapsing the browser to the task bar and after 5 minutes (or so), the console is showing messages that are being shown when my component gets initiated the first time, so: 我在Google Chrome (newest)下遇到了一种奇怪的情况-我正在导航到ng2驱动的Web应用程序中的列表,将浏览器折叠到任务栏,然后在5分钟(左右)后,控制台显示了以下消息:在我的组件首次启动时显示,因此:

public ngOnInit(): void {
    console.log("INIT CALLED");
}

So it looks like Chrome is killing my application, then it gets resurrected and is showing this INIT CALLED message. 因此,看来Chrome正在杀死我的应用程序,然后它被复活并显示此INIT CALLED消息。 What kills me is that ngOnDestroy hook is not called before ngOnInit gets called the second time. 使我ngOnDestroy是,在第二次调用ngOnInit之前没有调用ngOnDestroy挂钩。 Looks like a bug in ng2 for me, but has anybody experienced this and can share a fix with me? 对我来说,这似乎是ng2中的错误,但是有人遇到过这种情况,可以与我共享修复程序吗?

ngOnDestroy isn't called when the browser tab is closed or suspended. 当浏览器选项卡关闭或挂起时,不会调用ngOnDestroy
ngOnDestroy is only called when Angular removes a component. 仅当Angular删除组件时才调用ngOnDestroy

When the tab is closed, Angular doesn't get a chance to do anything. 关闭选项卡后,Angular将没有机会做任何事情。 See How can we detect when user closes browser? 请参阅我们如何检测用户何时关闭浏览器? about how to invoke code when the browser tab is closed. 有关关闭浏览器选项卡时如何调用代码的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM