TypeError:this.networkService.getRequest...pipe不是离子Angular中的函数

问题描述

单击选项卡栏中的第二个选项卡将显示错误

core.js:6241 ERROR Error: Uncaught (in promise): TypeError: this.networkService.getRequest(...).pipe is not a function
TypeError: this.networkService.getRequest(...).pipe is not a function
at PaymentService.getPaymentList (payment.service.ts:359)
at PaymentComponent.loadDataPaymentState (payment.component.ts:188)
at PaymentComponent.ngAfterViewInit (payment.component.ts:130)

这些标签中的两个页面正在使用PaymentComponent。一切都在首先打开的选项卡中起作用。单击第二个选项卡会导致错误

发生错误代码是:

getpaymentList() {
let list = this.buildListURL();
return this.networkService.getRequest(list).pipe(
  tap(
    (data) => {
      console.log(data);
   
      this.lastbill= data["bill"];
    },(error) => {
      console.log(" error");
      console.log(error);
    }
  )
);

}

getRequest的代码

getRequest(path,customHeader?) {
let options = this.createOptions();
if (customHeader) {
  options.headers = this.jsonConcat(options.headers,customHeader);
}

if (!this.isOnline) {
  this.errorOccurred(null);
}
let cachedData = this.cacheService.getCachednode(path);
if (cachedData) {
  return cachedData;
} else {
  return this.http
    .get(
      path,options
    )
    .pipe(
      map(
        (data) => {
          console.log("data");
          return data;
        },(error) => {
          console.log("error");
          console.log(error);
          this.errorOccurred(error);
        }
      )
    );
}

}

我无法确定问题所在。如果有人对此有任何想法,请告诉我。谢谢

解决方法

您的代码给出了错误,因为当您第一次调用 getRequest 方法时,调用方法期望可观察到,缓存中没有数据,并且 else子句中的数据被调用,但是下次调用此函数时,缓存中有一个数据,并且 if(cachedData)子句被调用,但是

if (cachedData) {
   return cachedData;
}  

未返回可观察值。

如果您要在其中做一些小改动,那么它也可以使用缓存代码。您必须执行以下操作:

  1. 第一个:

    import { of } from "rxjs";
    
  2. getRequest 方法进行的更改很少,而不是

    if (cachedData) {
       return cachedData;
    }   
    

   if (cachedData) {
       return of(cachedData);
   }