在const angular上创建通用类型

时间:2019-04-10 10:18:08

标签: angular rxjs rxjs-pipeable-operators

我在错误处理程序方面遇到问题。我正在尝试在服务中创建一个通用的retryPipeline:当调用失败时,它将在抛出错误之前重试3次。到目前为止,一切都很好。如果将代码放入方法中,则可以正常工作,

  getMun(id_del: number, id_muno: number): Observable<Mun> {

    let urlAPIMun = urlAPI;
    urlAPIMun += '/' + id_del + '/mun' + '/' + id_mun + '?flag_geometry=true';
    return this._http.get<Mun>(urlAPIMunicipios).pipe(
     //   tap(() => console.log('HTTP request executed')),
      retryWhen(errors => errors.pipe(
        // Concat map to keep the errors in order and make sure they
        // aren't executed in parallel
        concatMap((e: HttpErrorResponse, i) =>
          // Executes a conditional Observable depending on the result
          // of the first argument
          iif(
            () => i >= 3,
            // If the condition is true we throw the error (the last error)
            throwError(e.error.errores),
            // Otherwise we pipe this back into our stream and delay the retry
            of(e).pipe(delay(5000))
          )
        ))));
  }

我试图在管道中引出代码以声明一个const,然后在我的服务调用中调用const:

const RETRYPIPELINE =
  retryWhen((errors) =>
    errors.pipe(
      concatMap((e: HttpErrorResponse, i) =>
          () => i >= 3,
          throwError(e.error.errores),
          of(e).pipe(delay(5000))
        )
      )
    )
  );

return this._http.get<Mun>(urlAPIMunicipios).pipe(RETRYPIPELINE);

但是我收到此错误:

  

错误TS2322:类型'Observable <{}>'无法分配给类型'Observable'。         类型“ {}”缺少类型“ Mun”中的以下属性:id_mun,id_del,den

有什么方法可以创建一个通用的const,尽管该方法返回一个类型化的值,但它可以赋值给任何方法?预先感谢

1 个答案:

答案 0 :(得分:0)

最后,我感谢this answer

retryWhen中添加演员表,可以彻底解决我的问题:

export const RETRYPIPELINE =
  retryWhen<any>((errors) =>
    errors.pipe(
      // Use concat map to keep the errors in order and make sure they
      // aren't executed in parallel
      concatMap((e: HttpErrorResponse, i) =>
        // Executes a conditional Observable depending on the result
        // of the first argument
        iif(
          () => i >= 3,
          // If the condition is true we throw the error (the last error)
          throwError(e.error.errores),
          // Otherwise we pipe this back into our stream and delay the retry
          of(e).pipe(delay(5000))
        )
      )
    )
  )

;