2017-09-14 16 views
0

私はAngular 4フォームのcanDeactivateハンドラを作成しました。これは動作しますが、私はメモリリークを引き起こすことを恐れています(より良い言葉の欠如のため)。私は購読の絶えず増加するリストを避けるためにあなたが購読を解除しなければならないことを読んだ。しかし、私は以下で作成した定期購読からどこで/どのように購読を解除するかわかりません。 observer.completeの呼び出しでサブスクリプションが削除されますか?オブザーバのサブスクリプションを削除します

// Allow the user to navigate away from this page 
    public canDeactivate(): Observable<boolean> { 

    // Popup a prompt dialog 
    const title = 'Lose Changes'; 
    const prompt = 'Are you sure you want to lose your changes?'; 
    this.dialogWindow.show(EDialogTypes.EDialogYesNo, EDialogStyles.EDialogStyleWarning, title, prompt); 

    return Observable.create(observer => { 
     this.dialogWindow.observable.subscribe(buttonPressed => { 
     const proceed = (buttonPressed === EButtonPressed.EButtonPressedYes); 
     console.log('Allow proceed: ' + proceed); 
     observer.next(proceed); 
     observer.complete(); 
     }); 
    }); 
    } 

答えて

0

サブスクリプションを変数に保存して、必要なものを取得した後でサブスクリプションを解除することができます。このようなもの:

return Observable.create(observer => { 
    let subscription = this.dialogWindow.observable.subscribe(buttonPressed => { 
    const proceed = (buttonPressed === EButtonPressed.EButtonPressedYes); 
    console.log('Allow proceed: ' + proceed); 
    observer.next(proceed); 
    observer.complete() 
    subscription.unsubscribe(); // here you unsubscribe 
    }); 
}); 
関連する問題