Angular 4 Error: No provider for HttpClient
Angular 4 Error: No provider for HttpClient
我正在使用 CLI 开始一个新的 angular 项目,并且 运行 因 HttpClient
错误而成为无提供者。
我已将 HttpClientModule
添加到我的模块导入中,这似乎是这种情况下的常见罪魁祸首。除此之外,在网上找不到很多东西,所以我不确定可能是什么问题。
我的app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
和我的服务
@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);
}
}
和 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
我的 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"
]
}
}
我的测试
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();
}));
非常感谢任何帮助!
在你的测试中
TestBed.configureTestingModule({
providers: [FlexSearchService, HttpClientModule]
});
应该是
TestBed.configureTestingModule({
imports: [HttpClientModule],
providers: [FlexSearchService]
});
甚至更好(如果你想模拟请求):
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [FlexSearchService]
});
一种更简单的方法是在全球范围内提供它....将以下内容导入 app.module.ts,如下所示:
import { HttpModule } from '@angular/http'
import { HttpClient, HttpClientModule } from '@angular/common/http';
并在进口申报:
imports: [
HttpModule,
HttpClientModule, ...
]
导入 HttpClientTestingModule。
在你的测试中:
import { HttpClientTestingModule } from '@angular/common/http/testing';
并在测试的 configureTestingModule 中,执行以下操作:
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
})
.compileComponents();
为此,您必须导入以下内容:
import { HttpClientTestingModule } from '@angular/common/http/testing';
对于测试 configureTestingModule
中的规范文件,请执行以下操作:
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
})
.compileComponents();
我在尝试使用 npm link.
在我的项目中本地 link 我的 angular 库时注意到了这个问题
从存储库下载库解决了我的问题。
我正在使用 CLI 开始一个新的 angular 项目,并且 运行 因 HttpClient
错误而成为无提供者。
我已将 HttpClientModule
添加到我的模块导入中,这似乎是这种情况下的常见罪魁祸首。除此之外,在网上找不到很多东西,所以我不确定可能是什么问题。
我的app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
和我的服务
@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);
}
}
和 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
我的 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"
]
}
}
我的测试
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();
}));
非常感谢任何帮助!
在你的测试中
TestBed.configureTestingModule({
providers: [FlexSearchService, HttpClientModule]
});
应该是
TestBed.configureTestingModule({
imports: [HttpClientModule],
providers: [FlexSearchService]
});
甚至更好(如果你想模拟请求):
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [FlexSearchService]
});
一种更简单的方法是在全球范围内提供它....将以下内容导入 app.module.ts,如下所示:
import { HttpModule } from '@angular/http'
import { HttpClient, HttpClientModule } from '@angular/common/http';
并在进口申报:
imports: [
HttpModule,
HttpClientModule, ...
]
导入 HttpClientTestingModule。
在你的测试中:
import { HttpClientTestingModule } from '@angular/common/http/testing';
并在测试的 configureTestingModule 中,执行以下操作:
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
})
.compileComponents();
为此,您必须导入以下内容:
import { HttpClientTestingModule } from '@angular/common/http/testing';
对于测试 configureTestingModule
中的规范文件,请执行以下操作:
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
})
.compileComponents();
我在尝试使用 npm link.
在我的项目中本地 link 我的 angular 库时注意到了这个问题从存储库下载库解决了我的问题。