本文介绍了角度2-刷新或刚离开组件导航时会调用ngOnDestroy吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定Angular 2中的ngOnDestroy是刷新运行还是仅在有人离开页面时运行?

I am trying to figure out if ngOnDestroy in Angular 2 gets ran on a refresh or just when someone navigates away from the page?

推荐答案

在刷新时或当您离开当前页面导航时,将不会调用ngOnDestroy.该应用程序将被浏览器破坏.

On refresh or when you navigate away from the current page, then ngOnDestroy won't be called. The application will just be destroyed by the browser.

仅当Angular2因为您移开或在动态创建的组件上调用destroy()而从DOM中删除该组件时,才会调用ngOnDestroy().

Only when Angular2 removes the component from the DOM because you move away or you call destroy() on a dynamically created component, then ngOnDestroy() is called.

如果在浏览器销毁应用程序之前需要采取一些措施,则可以自己收听beforeunloadunload.

You can listen to beforeunload and unload yourself if you need some action to happen before the application is destroyed by the browser.

另请参见

  • https://developer.mozilla.org/en-US/docs/Web/Events/unload
  • How can we detect when user closes browser? (Angular)

这篇关于角度2-刷新或刚离开组件导航时会调用ngOnDestroy吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:42