Angular 4错误:没有HttpClient的提供程序

mrp*_*nik 41 angular angular-httpclient

我正在使用CLI启动一个新的角度项目,并且我遇到了HttpClient错误的无提供程序.

我已经添加HttpClientModule到我的模块导入,这似乎是这种情况下的常见罪魁祸首.除此之外没有找到很多在线,所以我不确定问题是什么.

我的app.module.ts

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

和我的服务

@Injectable()
export class FlexSearchService {


    constructor(private http: HttpClient) {}

    getSavedSearchResult(index: string, queryName: string, query: string ): Observable<any> {
      const url = `${environment.flexUrl}/${index}/search`;
      const item = new SearchQuery({queryName: queryName, variables: {q: query}});
      return this.http.post(url, item);
    }
}
Run Code Online (Sandbox Code Playgroud)

和ng版本提供以下输出

@angular/cli: 1.4.2
node: 6.9.4
os: darwin x64
@angular/animations: 4.4.4
@angular/common: 4.4.4
@angular/compiler: 4.4.4
@angular/core: 4.4.4
@angular/forms: 4.4.4
@angular/http: 4.4.4
@angular/platform-browser: 4.4.4
@angular/platform-browser-dynamic: 4.4.4
@angular/router: 4.4.4
@angular/cli: 1.4.2
@angular/compiler-cli: 4.4.4
@angular/language-service: 4.4.4
typescript: 2.3.4
Run Code Online (Sandbox Code Playgroud)

我的tsconfig

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我的考试

import { TestBed, inject } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { FlexSearchService } from './flex-search.service';

describe('FlexSearchService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [FlexSearchService, HttpClientModule]
    });
  });
  it('should be created', inject([FlexSearchService], (service: FlexSearchService) => {
    expect(service).toBeTruthy();
  }));
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

Kev*_*yon 67

在你的测试中

TestBed.configureTestingModule({
      providers: [FlexSearchService, HttpClientModule]
    });
Run Code Online (Sandbox Code Playgroud)

它应该是

TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [FlexSearchService]
    });
Run Code Online (Sandbox Code Playgroud)

甚至更好(如果你想模拟请求):

TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [FlexSearchService]
    });
Run Code Online (Sandbox Code Playgroud)


Mik*_*xle 10

一种更简单的方法是全局提供....将以下内容导入app.module.ts,如下所示:

import { HttpModule } from '@angular/http'
import { HttpClient, HttpClientModule } from '@angular/common/http';
Run Code Online (Sandbox Code Playgroud)

并在进口中声明:

  imports: [
    HttpModule,
    HttpClientModule, ...
]
Run Code Online (Sandbox Code Playgroud)


Mwi*_*iza 10

导入HttpClientTestingModule。

在您的测试中:

import { HttpClientTestingModule } from '@angular/common/http/testing';
Run Code Online (Sandbox Code Playgroud)

在测试的configureTestingModule中,执行以下操作:

TestBed.configureTestingModule({
    imports: [ HttpClientTestingModule ],
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)


小智 9

为此,您必须导入以下内容:

import { HttpClientTestingModule } from '@angular/common/http/testing';
Run Code Online (Sandbox Code Playgroud)

对于测试中的规范文件configureTestingModule,请执行以下操作:

TestBed.configureTestingModule({
    imports: [ HttpClientTestingModule ],
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)