The Angular documentation says
ngOnDestroy(): Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks. Called just before Angular destroys the directive/component.
Some developers are saying that the component properties (class variables) should also be set to null to avoid memory leaks. Is this true?
export class TestComponent implements OnInit, OnDestroy {
public name: string;
constructor() { }
ngOnInit() {
this.name = 'John';
}
ngOnDestroy() {
// is this code really necessary.
this.name = null;
}
}
@Input() nodeChanged: Subject<void>;
and the following codengOnInit() { this.nodeChanged$ = this.nodeChanged.subscribe(() => ... ) }
. In this case I had to set this.nodeChanged to null in ngOnDestroy like songOnDestroy() { this.nodeChanged = null; this.nodeChanged.unsubscribe();}
to avoid the ObjectUnsubscribedError – Terrieterrier