rxjs - 如何在离子事件中调用链订阅(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);
})
);
}
我有一个扩展 BaseDataService 的 LocationDeviceDataService,它有一个 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/)为“connection-type:wifi”发布时, this.locationDataService.getAll().subscribe 返回响应 1x ,2x,4x 等较慢。
我确信后端没有错。
应该取消订阅还是完成订阅?如果我应该,我没有任何触发器。
你能告诉我这段代码有什么问题吗?
解决方案
我解决了我的问题。
我认为您不能在 Ionic 事件中调用 observable 方法,所以我将方法更改为 void。目前一切都很好。
推荐阅读
- kubernetes - Kustomize 将一个补丁应用于多种资源类型(添加 ownerReferences)
- c# - 将 ASP.Net 添加到现有项目
- python - 图像分割后的数据增强
- opencv - 基于(放大)OpenCV 风格相机参数的径向失真误差
- huawei-mobile-services - 在 HMS 插件文档中,您讨论了 HMS 管理器预制件......但是在您的 HMS 插件内容中没有名为“HMS 管理器”预制件的预制件
- kotlin - 在 Kotlin 中使用成员函数打印对象属性
- c - 打印共享内存的 C 指针
- c++ - 数组大小变量的值发生了变化,而我没有在我的 c++ 合并排序代码中显式地更改它
- sql - 比较本地数据库和远程数据库的表
- excel - 使用密码从下拉列表中取消隐藏 Excel 工作表