Take this code for example:
@Component({
selector: 'some-chart',
template: '<div></div>,
})
class ChartingThing implements OnInit {
mySubscription: Subscription = new Subscription();
ngOnInit() {
this.mySubscription.subscribe((blah) => {
console.log(blah);
});
}
}
When Angular cleans up this component, will that automatically destroy the subscription? Or do I need to call this.mySubscription.unsubscribe()
in an ngOnDestroy method? I could see needing to call unsubscribe if Angular is caching components or something of that nature.
This is not the same as: Angular/RxJs When should I unsubscribe from `Subscription`
In every example there, they're talking about unsubscribing from an injected service. That makes sense to do since the service isn't necessarily going to be destroyed just because the component is destroyed.
Here, I am clearly referencing a subject created by this class. I want to know if mySubscription
needs to be completed
in ngOnDestroy or if it gets cleaned up because the class is destroyed and the mySubscription property would get destroyed with it or something else entirely.