我正在使用Angular 5编写应用程序。
我正在使用离子刷新器刷新页面,并且如果有错误或成功返回,那么我想取消微调器,因此本质上是finally块。
我需要刷新以停止旋转,并在任何结果(成功或错误)中返回默认位置。
如何用我的代码实现?这是我尝试过的,但出现错误:
html
<ion-content>
<ion-refresher slot="fixed" (ionRefresh)="refresh($event)"></ion-refresher>
<div *ngIf="data">
{{ data }}
</div>
</ion-content>
组件
export class UserInfoComponent implements OnInit {
data: any;
constructor(private userInfoService: UserInfoService) {}
ngOnInit() {
this.userInfoService
.getEmployeeInfo()
.subscribe((response) => {
this.data = response;
});
}
refresh(event) {
this.userInfoService
.getEmployeeInfo()
//.pipe(
//.finally(() => event.complete())
//)
.subscribe((response) => {
this.data = response;
event.complete(); //this works but what if there was an error
})
//.finally(() => event.complete());
//Property 'finally' does not exist on type 'Subscription'.
}
}
答案 0 :(得分:4)
使用subscribe
时:
.subscribe(
(response) => {
// do something when data is received
console.log(response);
}),
(err) => {
// do something on error
console.error(err)
},
() => {
// do something when operation successfully complete
console.log('success!);
});
答案 1 :(得分:1)
您需要在finalize
方法之前投放finally
而不是subscribe
广告。
.getEmployeeInfo()
.pipe(finalize(() => {
event.complete()
}))
是的,没有点.
,这对您有帮助