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

21 浏览
0 Comments

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

我将我的Angular应用程序从5.1.1升级到6,并遇到了一些与HttpClient和其他内容相关的问题。一旦我启动我的应用程序,就会出现错误。我在谷歌上搜索了这个问题,但没有找到完美的解决方案。有人能告诉我正确的解决方法应该是什么吗?

错误信息:

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