Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 8使用HttpInterceptor添加自定义请求头_Angular_Http Headers - Fatal编程技术网

Angular 8使用HttpInterceptor添加自定义请求头

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({

在angular 8中,添加自定义标头的HttpInterceptor不起作用 我需要使用HttpInterceptor将自定义请求头添加到我的http函数中

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

你好,阿塔,你介意格式化你的代码吗?我第一次为你做了,但是你改变了它…谢谢,但是我返回了新的请求,它包含了额外的头,但是仍然不起作用