[英]How to pass a generic type and create instance of this type in angular 2 component
[英]Create generic type on const angular
我遇到錯誤處理程序的問題。 我正在嘗試在我的服務中創建一個通用的retryPipeline:當調用失敗時,它會在throw和error之前重試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,它能夠與任何方法一致,雖然該方法返回一個類型值? 提前致謝
最后,我修復了這個答案 :
在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.