'Should I unsubscribe from observable defined in component?
I have an Angular component with observable (BehaviourSubject) set as a class member.
At some point I subscribe to it.
The question is: should I unsubscribe from it in ngOnDestroy() or not?
It's unclear since the lifetime of observable seems to be the same as a lifetime of the component and probably we shouldn't care about memory leak.
Example code:
@Component(...)
class MyComponent implements OnInit {
public subject: BehaviorSubject<string> = new BehaviorSubject('');
public ngOnInit(): void {
this.subject.subscribe(...);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
