我正在开发角度应用程序,并且尝试使用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函数是造成这种情况的原因,但我不确定。
如果发生错误,则不调用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)
});
}