タグ付けされた質問 「angular-component-life-cycle」

21
Angular / RxJs `Subscription`からの退会のタイミング
NgOnDestroyライフサイクル中にSubscriptionインスタンスを保存して呼び出す必要があるのunsubscribe()はいつですか。 すべてのサブスクリプションを保存すると、コンポーネントコードに多くの混乱が生じます。 HTTPクライアントガイドは、次のようなサブスクリプションを無視します。 getHeroes() { this.heroService.getHeroes() .subscribe( heroes => this.heroes = heroes, error => this.errorMessage = <any>error); } 同時に、Route&Navigation Guideによると: 最終的には、別の場所に移動します。ルーターはこのコンポーネントをDOMから削除して破棄します。それが起こる前に、私たちは自分自身を片付ける必要があります。具体的には、Angularがコンポーネントを破棄する前にサブスクライブを解除する必要があります。そうしないと、メモリリークが発生する可能性があります。 メソッドObservable内のサブスクライブを解除しますngOnDestroy。 private sub: any; ngOnInit() { this.sub = this.route.params.subscribe(params => { let id = +params['id']; // (+) converts string 'id' to a number this.service.getHero(id).then(hero => this.hero = hero); }); …
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.