如何在离子事件中调用链订阅(RxJs)



我有一个BaseDataService类,它有一个用于HttpGet请求的方法。

protected Get<TResponse>(
endPoint: string
): Observable<BaseResponse<TResponse>> {
return this.httpClient.get<TResponse>(this.baseUrl + endPoint).pipe(
map(data => {
const response = <BaseResponse<TResponse>>{};
response.Data = data;
response.Errors = [];
response.HasError = false;
return response;
}),
catchError(errors => {
const response = <BaseResponse<TResponse>>{};
response.Errors = [];
response.Errors.push(errors.error);
response.HasError = true;
return of(response);
})
);
}

我有一个LocationDeviceDataService,它扩展了BaseDataService并且有一个Get-LocationDevices 的方法

getAll() {
return this.Get<BasePaginatedResponse<LocationDeviceResponse>>(
EndPoints.GET_LOCATIONDEVICES
);
}

我在事件内部调用这个方法,

this.events.subscribe("connection-type:wifi", () => {
this.locationDataService.getAll().subscribe(t => {
localStorage.setItem('LOCATION_DEVICES', JSON.stringify(t.Data.items))
});
});

第一次呼叫时一切都很好,但当发生其他事件时(https://ionicframework.com/docs/api/util/Events/)publish对于"连接类型:wifi">this.locationDataService.getAll((.subscribe返回的响应速度慢1x、2x、4x等。

我确信后端没有任何错误。

应该取消订阅还是完成订阅?如果我应该的话,我没有任何触发因素。

你能告诉我这个代码出了什么问题吗?

我解决了我的问题。

我认为你不能在离子事件中调用可观察的方法,所以我把我的方法改为void。现在一切都很好。

最新更新