My Angular application has many components, one is MyComponent and the component class of which is shown bellow:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
@Component({
selector: 'app-my',
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss']
})
export class MyComponent implements OnInit {
a = 'I still alive';
constructor() {}
ngOnInit() {
Observable.interval(1000).subscribe(x => console.log(this.a));
}
}
If I visit to MyComponent, the subscription starts as expected. Say, I now navigate away from MyComponent and MyComponent should now be destroyed. But still I can see the subscription survives (the console logs keeps on coming). What are the practical benifit of allowing subscription to survive after the host component (MyComponent) has been destroyed?
(If I want to unsubscribe, I can do it in the ngOnDestroy() method of MyComponent, but how to unsubscribe is not the point of discussion here)