Angular 8使用HttpInterceptor添加自定义请求头
在angular 8中,添加自定义标头的HttpInterceptor不起作用 我需要使用HttpInterceptor将自定义请求头添加到我的http函数中Angular 8使用HttpInterceptor添加自定义请求头,angular,http-headers,Angular,Http Headers,在angular 8中,添加自定义标头的HttpInterceptor不起作用 我需要使用HttpInterceptor将自定义请求头添加到我的http函数中 intercept( req: HttpRequest<any>, next: HttpHandler, ): Observable<HttpEvent<any>> { const newRequest = req.clone({
intercept(
req: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
const newRequest = req.clone({
headers: req.headers.set('APIKey', 'YOUR-API Key-HERE'),
});
return next.handle(newRequest);
}
从HttpInterceptor听到我的代码
intercept(
req: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
const newRequest = req.clone({
headers: req.headers.set('APIKey', 'YOUR-API Key-HERE'),
});
return next.handle(newRequest);
}
结果不正确,如下图所示
我需要它如下图所示
任何帮助更改
返回next.handlereq
返回包含附加标头的新请求
返回next.handlenewRequest 你好,阿塔,你介意格式化你的代码吗?我第一次为你做了,但是你改变了它…谢谢,但是我返回了新的请求,它包含了额外的头,但是仍然不起作用