![](/img/trans.png)
[英]TypeScript/RxJS - Observable subscribe() method complete() not running
[英]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.