When you subscribe to query params in a component, do you need to unsubscribe? I'm trying to avoid a memory leak.
Subscription with variable for unsubscribe()
subscription$: Subscription
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.subscription$ = this.route.queryParams.subscribe(
(params: any): void => {
// ... Do stuff here ...
}
)
}
ngOnDestroy() {
if (this.subscription$ !== undefined || this.subscription$ !== null) {
this.subscription$.unsubscribe()
}
}
Subscription without variable for unsubscribe()
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.queryParams.subscribe(
(params: any): void => {
// ... Do stuff here ...
}
)
}
Which one is a better solution?