[英]Complete not getting called in Observable subscribe method
I am working on the angular application and I am trying to use RxObservable.我正在研究 angular 应用程序,并且正在尝试使用 RxObservable。 Below is the sample code.下面是示例代码。
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;
}
});
}
Now this is an angular 2 app (Single page application).现在这是一个 angular 2 应用程序(单页应用程序)。 When I reload the page, the complete part of above function gets called and this.accountDataLoaded is true.当我重新加载页面时,上述函数的完整部分被调用并且 this.accountDataLoaded 为真。
However, If I move to other component and come back to this one, the complete is not getting called and accountDataLoaded stays false.但是,如果我移动到其他组件并回到这个组件,则不会调用 complete,并且 accountDataLoaded 保持为 false。
I see there's no error on the console as well since I am logging the error as you can see above.我看到控制台上也没有错误,因为我正在记录错误,如上所示。
I am thinking either filter or takeUntil function on that observable are causing that but I am not sure.我认为该 observable 上的 filter 或 takeUntil 函数会导致这种情况,但我不确定。
With RXjs behaviour complete is not been called if error is called.如果调用错误,则不会调用 RXjs 行为完成。 In your case you need do the following thing.在您的情况下,您需要执行以下操作。
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.