簡體   English   中英

在 Observable 訂閱方法中完全沒有被調用

[英]Complete not getting called in Observable subscribe method

我正在研究 angular 應用程序,並且正在嘗試使用 RxObservable。 下面是示例代碼。

getMyData(){
 console.log('get my account data called');
  this.AccountService
    .getMyAccountData()
    .filter(_res => _res !== null)
    .takeUntil(this.ngUnsubscribe)
    .subscribe({
        next: _response => {console.log('call finished');}
        error: error => {
            console.log('had an error');
            handleHttpError(error);
       },
       complete: () => {
           console.log('account data loaded is being set');
           this.accountDataLoaded = true;
       }
    });
}

現在這是一個 angular 2 應用程序(單頁應用程序)。 當我重新加載頁面時,上述函數的完整部分被調用並且 this.accountDataLoaded 為真。

但是,如果我移動到其他組件並回到這個組件,則不會調用 complete,並且 accountDataLoaded 保持為 false。

我看到控制台上也沒有錯誤,因為我正在記錄錯誤,如上所示。

我認為該 observable 上的 filter 或 takeUntil 函數會導致這種情況,但我不確定。

如果調用錯誤,則不會調用 RXjs 行為完成。 在您的情況下,您需要執行以下操作。

  getMyData(){
 console.log('get my account data called');
  this.AccountService
    .getMyAccountData()
    .filter(_res => _res !== null)
    .takeUntil(this.ngUnsubscribe)
    .subscribe({
        next: _response => {console.log('call finished');}
        error: error => {
            console.log('had an error');
            handleHttpError(error);
       },
       complete: () => {
           console.log('account data loaded is being set');
           this.accountDataLoaded = true;
       }
    }).add(() => {
         //Called when operation is complete (both success and error)
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM