2017-12-15 25 views
-1

私は、観察可能な部分のsubscribe部分の中でいくつかのリダイレクトを行っています。ユースケースは、まずアイテムをロードし、最初のアイテムを自動的に選択してURLにリダイレクトする必要があるということです。だから、ここのコードです:すべての.subscribe内でリダイレクトする - メモリリークの原因になりますか?

this.categories$.takeUntil(this.unsubscribe).subscribe((mainCategories: ICategory[]) => { 
    if (<number>mainCategories.length > 0) { 
    this.url.parent = <string>mainCategories[0].TermCategoryUrl; 
    this.router.navigate([this.url.link]); 
    } 
}); 

は今、この作品と、それは任意のメモリリークを引き起こしている場合、私は不思議でしたか?私はtakeUntilの助けを退会するが、私はリダイレクトを行うときにこれが呼び出されるかどうか分からない?

ngOnDestroy(): void { 
    this.unsubscribe.next(); 
    this.unsubscribe.complete(); 
} 

ここで、安全で安全なシナリオは何でしょうか?

答えて

-1

どのように購読を解除するのかわかりませんが、問題を確認したい場合は、[ネットワーク]タブに移動し、まだ購読しているイベントのデータを受信して​​いるかどうかを確認してください。あなたは、いくつかのサブスクリプションをお持ちの場合

そうでない場合は、なぜあなたは単に

let subscription = this.categories$.subscribe(...); 


ngOnDestroy() { 
    this.subscription.unsubscribe(); 
} 

EDITをしない、あなたはこの

export abstract class SubManager implements OnDestroy { 

    private subscribers: Subscription[]; 

    constructor() { this.subscribers = []; } 

    ngOnDestroy() { this.removeSubscriptions(); } 

    protected addSubscription(sub: Subscription) { this.subscribers.push(sub); } 

    private removeSubscriptions() { 
    for (const sub of this.subscribers) { 
     sub.unsubscribe(); 
    } 
    this.subscribers = null; 
    delete (this.subscribers); 
    } 
} 

のようなクラスを実装することができ、あなたのクラスは、これを拡張しますクラス

export class AppComponent extends SubManager implements OnInit { 
    constructor() { 
    super(); 
    this.addSubscription(this.categories$.subscribe(...)); 
    } 
+0

私はいくつかの異なるサブスクリプションを持っていますsと私は 'void'観測値を作成し、' ngDestroy'で 'next'を呼び出すほうが簡単だということを読んでいます - 他のすべての' subscribe'の前に 'takeUntil'を置くだけです。出典:https://alligator.io/angular/takeuntil-rxjs-unsubscribe/ – uglycode

+0

あなたが知っている、あなたはそれを私の編集のように単純にしますか? – trichetriche

+0

OKですが、実際に私の主な問題を解決したり解決したりするわけではありません。つまり、メモリリークを引き起こすことなく、観測可能なリダイレクトを行うことができますか? – uglycode

関連する問題