Est*_*ask 44 angular angular4-httpclient
通常,希望具有将应用于所有请求的默认超时(例如30秒),并且可以针对特定的更长请求(例如600s)覆盖.
据Http我所知,没有很好的方法来指定默认超时.
有什么方法可以解决这个问题HttpClient?如何为所有传出请求定义默认超时,可以覆盖特定的超时?
Est*_*ask 88
看来,不扩展HttpClientModule类,拦截器的唯一预期的沟通方式与相应的请求是params和headers对象.
由于超时值是标量,因此可以安全地将其作为自定义标头提供给拦截器,在此可以确定是否应该通过RxJS timeout运算符应用它的默认或特定超时:
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { timeout } from 'rxjs/operators';
export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
@Injectable()
export class TimeoutInterceptor implements HttpInterceptor {
constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const timeoutValue = req.headers.get('timeout') || this.defaultTimeout;
const timeoutValueNumeric = Number(timeoutValue);
return next.handle(req).pipe(timeout(timeoutValueNumeric));
}
}
Run Code Online (Sandbox Code Playgroud)
这可以在您的应用模块中配置,如:
...
providers: [
[{ provide: HTTP_INTERCEPTORS, useClass: TimeoutInterceptor, multi: true }],
[{ provide: DEFAULT_TIMEOUT, useValue: 30000 }]
],
...
Run Code Online (Sandbox Code Playgroud)
然后使用自定义timeout标头完成请求
http.get(..., { headers: new HttpHeaders({ timeout: `${20000}` }) });
Run Code Online (Sandbox Code Playgroud)
由于标头应该是字符串,因此应首先将超时值转换为字符串.
这是一个演示.
积分去@RahulSingh和@ Jota.Toledo建议使用拦截器的想法timeout.
Mar*_*rio 21
作为对其他答案的补充,请注意,如果您在开发机器上使用代理配置,则代理的默认超时为 120 秒(2 分钟)。对于更长的请求,您需要在配置中定义更高的值,否则这些答案都不起作用。
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"timeout": 360000
}
}
Run Code Online (Sandbox Code Playgroud)
Rah*_*ngh 10
使用新的HttpClient,您可以尝试这样的事情
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).timeout(5000).do(event => {}, err => { // timeout of 5000 ms
if(err instanceof HttpErrorResponse){
console.log("Error Caught By Interceptor");
//Observable.throw(err);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
向next.handle(req)传递的超时添加超时.
在AppModule中注册它就像
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
[ { provide: HTTP_INTERCEPTORS, useClass:
AngularInterceptor, multi: true } ]
],
bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
Jot*_*edo 10
您可以使用基本超时值创建全局拦截器,如下所示:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).timeout(30000, Observable.throw("Request timed out"));
// 30000 (30s) would be the global default for example
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您需要在根模块的providers数组中注册此injectable.
棘手的部分是覆盖特定请求的默认时间(增加/减少).目前我不知道如何解决这个问题.
| 归档时间: |
|
| 查看次数: |
45982 次 |
| 最近记录: |