【发布时间】:2018-11-05 15:19:41
【问题描述】:
我有一个 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))
});
});
第一次调用一切正常,但是当 "connection-type:wifi"publish 发生另一个事件时 (https://ionicframework.com/docs/api/util/Events/) em> this.locationDataService.getAll().subscribe 返回响应慢 1x、2x、4x 等。
我确信后端没有错。
应该取消订阅还是完成订阅?如果我应该这样做,我没有任何触发因素。
你能告诉我这段代码有什么问题吗?
【问题讨论】:
-
this.events是什么?你确定它的subscribe方法将字符串作为第一个参数吗? -
@martin ionicframework.com/docs/api/util/Events ,是的,它将事件名称作为字符串
标签: rxjs observable subscribe ionic4 ionic-events