关于 Angular Component ngOnDestroy 钩子函数的调用时机
我開發了一個 Angular Component,實現了 ngOnDestroy 鉤子方法之后,沒有找到其被調用的方法。查看這個StackOverflow鏈接后,得知無論是我刷新瀏覽器,還是從當前頁面路由到一個新的頁面,都不會觸發 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.
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.
只有當 Component 從 DOM 中被移除,或者顯式調用一個動態創建的 Component 實例的 destroy 方法,才能觸發 ngOnDestroy.
我點擊 navigate to 跳轉之前,DOM 元素如下圖所示:
點了跳轉后,browser-test 元素確實從 DOM 中消失了:
然而我在該 DOM 節點上設置的 Break on node removal 并沒有觸發,感覺這個跳轉像是一次頁面刷新:
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的关于 Angular Component ngOnDestroy 钩子函数的调用时机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 真金怎么鉴别 如何鉴别真金
- 下一篇: Angular 动态控制 aside 标