【发布时间】:2019-12-07 10:00:07
【问题描述】:
我想为我的 Angular 8 应用程序中的所有 HTTPClients 添加标头。这是我的拦截器:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class Interceptor implements HttpInterceptor {
constructor(private toaster: ToastrService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
alert('Interceptor!');
// Set headers
const headers = req.headers;
// Set this header for security
headers.set('test', 'value');
const authReq = req.clone({ headers });
return next.handle(authReq);
}
}
警报已执行,但测试标头未添加到请求中。
【问题讨论】:
标签: angular httpclient angular-http-interceptors