【问题标题】:How to call chain subscription (RxJs) in ionic events如何在离子事件中调用链订阅(RxJs)
【发布时间】: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 等。

我确信后端没有错。

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

你能告诉我这段代码有什么问题吗?

【问题讨论】:

标签: rxjs observable subscribe ionic4 ionic-events


【解决方案1】:

我解决了我的问题。

我认为您不能在 Ionic 事件中调用 observable 方法,所以我将方法更改为 void。目前一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2021-01-28
    相关资源
    最近更新 更多