在 const angular 上创建泛型
Create generic type on const angular
我在错误处理程序方面遇到了问题。我正在尝试在我的服务中创建一个通用的 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 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);
但是我收到了这个错误:
error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable'.
Type '{}' is missing the following properties from type 'Mun': id_mun, id_del, den
有没有什么方法可以创建一个通用的 const,它可以赋值给任何方法,尽管方法 returns 是一个类型化的值?提前致谢
最后,感谢,我修复了它:
在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))
)
)
)
)
;
我在错误处理程序方面遇到了问题。我正在尝试在我的服务中创建一个通用的 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 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);
但是我收到了这个错误:
error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable'. Type '{}' is missing the following properties from type 'Mun': id_mun, id_del, den
有没有什么方法可以创建一个通用的 const,它可以赋值给任何方法,尽管方法 returns 是一个类型化的值?提前致谢
最后,感谢
在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))
)
)
)
)
;