1Observables are a building block of Angular but can lead to problems if they are not used properly. Given the following context, which line(s) describe(s) a good implementation to unsubscribe from an Observable: (the provided implementation is not optimal, it is presentational only)
@Component ({
selector: 'my-comp',
template: '<div>Hi , {{someObs | async }}>' // line 1
})
export class XComponent implements OnInit, OnDestroy {
someObs$: Observable<any>;
private unsub: Subscription;
private subject: Subject = new Subject<void> ();
constructor (private someService: SomeService) {}
ngOnInit () {
this.someObs$ = this.someService.someMethod().pipe(
takeUntil(this.subject)); // line 2
this.unsub = this.someObs$.subscribe(); // line 3
}
ngOnDestoy () {
if(this.unsub) {
this.unsub.unsubscribe();
}
this.subject.next();
this.subject.complete();
}
}