私は次のコンポーネントを持っています:
@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);
最もクリーンな方法は、takeUntil
後に呼び出すことswitchMap
です。
update$.pipe(
switchMap(() => this.service.get()),
takeUntil(this.destroy$)
).subscribe(result => this.result = result);
これにより、サブスクリプションへの排出が防止されます。takeUntil
以前に追加した場合、switchMap
サブスクリプションは、project
関数によって返されるオブザーバブルが完了するまで値を出力しますが、完了しない場合があります(サービスコードによって異なります)。
ソースからのサブスクライブ解除は、それ自体がサブスクライブ解除されたときに監視可能(以前のすべて)であるため、takeUntil
前後の両方を呼び出す必要はありません。switchMap
switchMap
switchMap
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加