Answer the question
In order to leave comments, you need to log in
How does unsubscribing work with takeUntil?
Please help me understand one point in the work of unsubscribing with takeUntil. Here's a classic example:
import { Component, OnDestroy, OnInit } from '@angular/core';
import 'rxjs/add/operator/takeUntil';
import { Subject } from 'rxjs/Subject';
import { MyThingService } from '../my-thing.service';
@Component({
selector: 'my-thing',
templateUrl: './my-thing.component.html'
})
export class MyThingComponent implements OnDestroy, OnInit {
private ngUnsubscribe: Subject<void> = new Subject<void>();
constructor(
private myThingService: MyThingService,
) { }
ngOnInit() {
this.myThingService.getThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
this.myThingService.getOtherThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
Answer the question
In order to leave comments, you need to log in
1) Do I understand correctly that the first line passes an empty value (but still a value) to the Subject and this causes the Subject to emit at least something?
All right. The code specifies the type
private ngUnsubscribe: Subject<void> = new Subject<void>();
, therefore, the typescript compiler will not allow anything other than an "empty" value to be passed to .next(). Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question