英文:
Do I need to Unsubscribe Router's event observable
问题
这意味着所有组件的成员也会被销毁。
它明确表示ActivatedRoute
不需要被取消订阅,但对于Router
的可观察对象(如events
),仍然存在困惑。
英文:
I have confusion with Angular docs statement, which says "This means all the component's members will also be destroyed"
so the question is do i need to unsubscribe it or not,
constructor(
private readonly router: Router
) {
this.router.events.subscribe((event: Event) => {
if (event instanceof NavigationEnd) {
It clearly says about ActivatedRoute
need not to be unsubscribed, but keeps the confusion for Router
observable like events
.
答案1
得分: 2
这是关于 ActivatedRoute
而不是 Router
的讨论,解决方法是执行以下操作:
eventSubscription: Subscription;
...
this.eventSubscription = this.router.events.subscribe((event: Event) => {
...
ngOnDestroy() {
this.eventSubscription.unsubscribe()
}
英文:
It is talking about ActivatedRoute
but not Router
the solution would be to do
eventSubscription:Subscription;
...
this.eventSubscription = this.router.events.subscribe((event: Event) => {
...
ngOnDestroy () {
this.eventSubscription.unsubscribe()
}
答案2
得分: 0
是的,您需要取消订阅路由器事件,因为Router
是全局可重用的提供程序,所以即使在组件销毁后,Router
仍将在DI中存在,带有所有订阅。
英文:
Yes, you need to usubscribe from Router events because Router
is global reusable provider, so even after the component destroy Router
will be living in DI with all subcriptions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论