在 Angular 7 组件中,我使用 RxJS takeUntil() 正确取消订阅可观察订阅。
this.destroy$.next()方法中缺少时会发生什么ngOnDestroy(参见下面的示例)?它仍然会正常退订吗?this.destroy$.complete()方法中缺少时会发生什么ngOnDestroy(参见下面的示例)?它仍然会正常退订吗?
@Component({
selector: 'app-flights',
templateUrl: './flights.component.html'
})
export class FlightsComponent implements OnDestroy, OnInit {
private readonly destroy$ = new Subject();
public flights: FlightModel[];
constructor(private readonly flightService: FlightService) { }
ngOnInit() {
this.flightService.getAll()
.pipe(takeUntil(this.destroy$))
.subscribe(flights => this.flights = flights);
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
Run Code Online (Sandbox Code Playgroud)