У меня есть следующий компонент:
@Component({...})
export class AppComponent implements OnInit, OnDestroy {
destroy$ = new Subject();
update$ = new Subject();
result: Result;
constructor(service: Service) {
}
ngOnInit() {
update$.pipe(
takeUntil(this.destroy$),
switchMap(() => this.service.get())
).subscribe(result => this.result = result);
this.refresh();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
refresh() {
this.update$.next();
}
}
Правильный ли этот подход? Или я должен позвонить takeUntil(this.destroy$)
после switchMap
?
update$.pipe(
switchMap(() => this.service.get()),
takeUntil(this.destroy$)
).subscribe(result => this.result = result);
Или я должен позвонить дважды?
update$.pipe(
takeUntil(this.destroy$),
switchMap(() => this.service.get()),
takeUntil(this.destroy$)
).subscribe(result => this.result = result);