在可观察的订阅方法中未完全调用

问题描述 投票:1回答:1

我正在开发角度应用程序,并且尝试使用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;
       }
    });
}

现在这是一个有角度的2应用程序(单页应用程序)。当我重新加载页面时,上述函数的完整部分将被调用,并且this.accountDataLoaded为true。

但是,如果我移至其他组件,然后再回到此组件,则不会调用完整组件,accountDataLoaded保持为假。

我看到控制台上也没有错误,因为如上所述,我正在记录错误。

我认为该可观察的对象上的filter或takeUntil函数是造成这种情况的原因,但我不确定。

javascript angular rxjs angular2-directives angular-observable
1个回答
0
投票

如果发生错误,则不调用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)
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.