如何将一个参数传递给HttpInterceptor?
我正在使用Angular 4.3.1和HttpClient。 有一个HttpInterceptor来设置一些头文件。
在一些http获取请求中,我需要设置不同的标题。 无论如何,我可以传递一些参数到这个HttpInterceptor为那个特定的HttpRequest?
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(request.custom.param1) // how can i do this
request = request.clone({
setHeaders: {
'header1': 'xxxxxx'
}
});
else
request = request.clone({
setHeaders: {
'header2': 'yyyyyy'
}
});
return next.handle(request);
}
}
也许有更好的方法来处理这个问题,但作为一种解决方法,你可以创建并传递自定义的HttpParams
来请求,然后在拦截器中检查它们。 例如:
export class CustomHttpParams extends HttpParams {
constructor(public param1: boolean) {
super();
}
}
在http调用中使用这个类:
this.http.get('https://example.com', {
params: new CustomHttpParams(true)
})
现在在拦截器中:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.params instanceof CustomHttpParams && request.params.param1)
request = request.clone({
setHeaders: {
'header1': 'xxxxxx'
}
});
else
request = request.clone({
setHeaders: {
'header2': 'yyyyyy'
}
});
return next.handle(request);
}
我编写了一个用于处理Http错误响应的拦截器。 我希望允许特定的Http调用指示拦截器忽略某些响应状态代码,同时保留将参数传递给Http调用的能力。 这是我结束的解决方案。 (谢谢,阿列克谢在你的回答中提供了最初的想法)。
扩展HttpParams:
import { HttpParams } from '@angular/common/http';
import { HttpParamsOptions } from '@angular/common/http/src/params';
// Cause the HttpErrorInterceptor to ignore certain error response status codes like this:
//
// this.http.get<TypeHere>(`URL_HERE`, {
// params: new InterceptorHttpParams({ statusCodesToIgnore: [400, 401] }, {
// complete: 'false',
// offset: '0',
// limit: '50'
// })
// })
export class InterceptorHttpParams extends HttpParams {
constructor(
public interceptorConfig: { statusCodesToIgnore: number[] },
params?: { [param: string]: string | string[] }
) {
super({ fromObject: params } as HttpParamsOptions);
}
}
拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap(
() => {},
(error: any) => {
if (error instanceof HttpErrorResponse) {
const regEx = /^[4-5][0-9][0-9]$/; // 4XX and 5XX status codes
if (regEx.test(error.status.toString())) {
const errorMessage = this.getErrorMessageFromStatus(error.status);
if (!this._shouldIgnoreError(req, error)) {
console.log(`ERROR INTERCEPTOR: ${error.status}`);
this.toastService.alert(errorMessage);
}
}
}
})
);
}
// Based on `request.params.interceptorConfig.statusCodesToIgnore`, we can see if we should ignore this error.
_shouldIgnoreError(request: HttpRequest<any>, errorResponse: HttpErrorResponse) {
if (request.params instanceof InterceptorHttpParams
&& Array.isArray(request.params.interceptorConfig.statusCodesToIgnore)
&& request.params.interceptorConfig.statusCodesToIgnore.includes(errorResponse.status)) {
return true;
}
return false;
}
链接地址: http://www.djcxy.com/p/40453.html
上一篇: How to pass a param to HttpInterceptor?
下一篇: How to put the first letter capitalized and the rest lowercase? [JS]