Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 侦听器未侦听http POST请求(6)_Angular_Angular6_Angular Http Interceptors - Fatal编程技术网

Angular 侦听器未侦听http POST请求(6)

Angular 侦听器未侦听http POST请求(6),angular,angular6,angular-http-interceptors,Angular,Angular6,Angular Http Interceptors,我已经创建了一个角度拦截器来实现httpinterceptor。对于http GET请求,它工作正常。但POST请求失败 我的拦截器代码如下 import { Injectable, Injector } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders,HttpResponse } from '@angular/common/http'

我已经创建了一个角度拦截器来实现httpinterceptor。对于http GET请求,它工作正常。但POST请求失败

我的拦截器代码如下

import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest,         HttpHeaders,HttpResponse } from '@angular/common/http';

import { Observable } from 'rxjs';

@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
  constructor() { console.log('enter constructor');
   }

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    console.log('entered interceptor')
    const token = localStorage.getItem('sessiontoken') != null ?     localStorage.getItem('sessiontoken') : 'notoken';
    const authReq = req.clone({ headers:     req.headers.set("Authorization", token) });

    return next.handle(authReq);

  }

}
在组件中,我导入了httpClient并使用了this.http.post

import { HttpClient } from '@angular/common/http';

我找到了答案,我将HttpClientModule导入到所有子组件的模块中。它应该仅在app.module.ts中导入。一旦我从每个文件中删除了所有HttpClientModule,问题就解决了,成功地将头绑定到每个请求。(不记得为什么我实际上不必要地导入了它,一些教程副本错误)


谢谢大家,干杯

为什么在同一个提供者声明上有useValue和useClass?我删除了它,但仍然不起作用。。。这节省了我几个小时的工作。为什么会这样?我已经在功能模块中的
forRoot
静态方法中导入了它,并修复了它。那是我唯一拥有它的地方。也为我工作
import { HttpClient } from '@angular/common/http';