import { Component, input } from '@angular/core';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
@Component({ ... })
class MyComponent {
value = input<string>();
constructor() {
toObservable(this.value)
.pipe(takeUntilDestroyed()) // is this superfluous?
.subscribe(console.log);
}
}
The documentation states that toObservable must be called in an injection context, so I am not sure whether it is automatically cleaning up subscriptions or not.
toObservableimplementation will use aDestroyRefto invokecompleteon the observable it creates. So in a sense yes it is not necessary to have atakeUntilDestroyed()on it iftoObservableis invoked in a component.However, if
toObservableis invoked in a service providedIn root,DestroyRefwill never fireonDestroyand your observable will (almost) never be destroyed.This example is a great illustration of an observable that wouldn't complete automatically.