关于HttpClient,HttpClientModule和HttpHeaders的一些奇怪的错误

时间:2018-06-29 11:36:49

标签: typescript http http-headers angular6

我已经将我的角度应用程序从5.1.1升级到了6,并且遇到了HttpClient和所有客户端的一些问题。启动我的应用程序后出现错误。我在Google上搜索了此问题,但没有找到完美的解决方案。有人可以告诉我什么是正确的方法吗?

错误:

AppComponent_Host.ngfactory.js? [sm]:1 ERROR Error: StaticInjectorError(AppModule)[UserStoreApiService -> HttpClientModule]: 
  StaticInjectorError(Platform: core)[UserStoreApiService -> HttpClientModule]: 
    NullInjectorError: No provider for HttpClientModule!
    at NullInjector../node_modules/@angular/core/fesm5/core.js.NullInjector.get (vendor.js:55249)

component.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpClientModule, HttpHeaders } from '@angular/common/http';
import { AuthHttp } from 'angular2-jwt';

import { ConfigService } from '../../config.service';

@Injectable()
export class UserStoreApiService {
  userStoreUrl: string;

  constructor(private http: AuthHttp,
              private _http: HttpClient,
              private config: ConfigService) {}

  private setUrl() {
    if (this.userStoreUrl === undefined) {
      this.userStoreUrl = this.config.get('userstore');
    }
  }

  public getUrl(apiUrl: string, headers: any) {
    this.setUrl();
    return this.http.get(this.userStoreUrl + apiUrl, headers);
  }

  public postUrl(apiUrl: string, data: string, header: any) {
    this.setUrl();
    let headers = new Headers();
    headers.append('authorization', 'Bearer ' + localStorage.getItem('access_token'));
    headers.append('Access-Control-Allow-Origin', '*');
    return this.http.post(this.userStoreUrl + apiUrl, data, { headers });
  }

  public putUrlWithFormData(apiUrl: string, data: any) {
    this.setUrl();
    let headers = new Headers();
    headers.append('authorization', 'Bearer ' + localStorage.getItem('access_token'));
    headers.append('Access-Control-Allow-Origin', '*');
    return this._http.put(this.userStoreUrl + apiUrl, data, {headers});
  }

  public putUrl(apiUrl: string, data: string, headers: any) {
    this.setUrl();
    return this.http.put(this.userStoreUrl + apiUrl, data, headers);
  }

  public deleteUrl(apiUrl: string, headers: any) {
    this.setUrl();
    return this.http.delete(this.userStoreUrl + apiUrl, headers);
  }

  public getRoot() {
    this.setUrl();
    return this.userStoreUrl;
  }
}

0 个答案:

没有答案