英文:
How to know if user left my component route in Angular 15?
问题
ngOnDestroy 在用户在应用程序的不同路由之间切换时不会触发。
是否有其他方法可以知道用户离开我的组件路由并销毁组件?
英文:
ngOnDestroy doesn't trigger when a user switches between different routes of the application.
Is there any other way to know if the user leaves my component route and destroy component then?
答案1
得分: 1
ngOnDestroy
在用户在应用程序的不同路由之间切换时不会触发。
这不是真的,当组件从 DOM 中移除时,ngOnDestroy()
会被调用,参见这个 Stackblitz。
当路由被更改(点击其中一个按钮),ngOnDestroy()
会在被移除的组件上调用。
你可以通过获取对组件的引用并调用 .destroy()
来手动销毁组件,但这通常不推荐。
英文:
> ngOnDestroy doesn't trigger when a user switches between different routes of the application.
That's not true, ngOnDestroy()
is called when the component is removed from DOM, see this Stackblitz.
When the route is changed (click one of the buttons), ngOnDestroy()
is called on the component that is being removed.
You can manually destroy a component by getting the reference to it and calling .destroy()
but that is generally not recommended.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论