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

时间:2018-11-05 15:19:41

标签: rxjs observable subscribe ionic4 ionic-events

我有一个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,并且具有用于获取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”的其他事件(https://ionicframework.com/docs/api/util/Events/ publishing 。this.locationDataService.getAll()。subscribe的响应速度会慢1x,2x,4x等。

我确信后端没有错。

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

能否请您告诉我这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。

我认为您无法在离子事件中调用可观察的方法,因此我将方法更改为void。现在一切都很好。