[英]Create generic type on const angular
I'm having problems with error handlers. 我遇到错误处理程序的问题。 I'm trying to create a generic retryPipeline in my service: when the call fails, it retry 3 times before throw and error.
我正在尝试在我的服务中创建一个通用的retryPipeline:当调用失败时,它会在throw和error之前重试3次。 So far so good.
到现在为止还挺好。 It works if I put the code inside the method, like this :
如果我把代码放在方法中,它会工作,如下所示:
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))
)
))));
}
I'm trying to extrac the code inside pipe to declare a const and then call the const in my service call: 我正在尝试提取管道内的代码来声明一个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);
But I'm receiving this error: 但是我收到了这个错误:
error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable'.
错误TS2322:类型'Observable <{}>'不能分配给'Observable'类型。 Type '{}' is missing the following properties from type 'Mun': id_mun, id_del, den
类型'{}'缺少类型'Mun'的以下属性:id_mun,id_del,den
Is there any way to create a generic const, that can be able to asign to any method, although the method returns a typed value? 有没有办法创建一个通用的const,它能够与任何方法一致,虽然该方法返回一个类型值? Thanks in advance
提前致谢
Finally, I fixed it thank to this answer : 最后,我修复了这个答案 :
Adding a cast in retryWhen
, definitively solve my problem: 在
retryWhen
添加一个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))
)
)
)
)
; ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.