在 Observable 订阅方法中完全没有被调用

rom*_*e99 8 javascript rxjs angular2-directives angular angular-observable

我正在开发角度应用程序,并尝试使用 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;
       }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

但是,如果我移至其他组件并返回到此组件,则不会调用完整组件,并且 accountDataLoaded 保持 false。

我看到控制台上也没有错误,因为我正在记录错误,如您在上面看到的那样。

我认为该可观察对象上的过滤器或 takeUntil 函数导致了这种情况,但我不确定。

Rah*_*ase 20

对于 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)
    });
}
Run Code Online (Sandbox Code Playgroud)