我是一个基于Angular的UI项目的新手,并使用vscode生成了两个新组件以供该项目使用。但是,每当我将更改推送到git(bitbucket)时,我都会不断收到错误,并且在我的机器上构建项目时不出现这些错误。我需要在哪里提供HttpClient / HttpClientModule,以便这些新组件可以使用HttpHandler?
我在app.module.ts和新组件本身上都添加了HttpClientModule和HttpClient的提供程序。
modal-wiped-all.component.ts
import { Component, OnInit } from '@angular/core';
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { OrganisationsApiService } from 'src/app/api/organisations/organisations-api.service';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';
@Component({
selector: 'app-modal-wipedall',
templateUrl: './modal-wiped-all.component.html',
providers: [NgbActiveModal,
OrganisationsApiService,
HttpClientModule,
HttpClient,
]})
cleanup.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';
import { OrganisationsApiService } from 'src/app/api/organisations/organisations-api.service';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ModalWipedAllComponent } from '../modal/modal-wiped-all/modal-wiped-all.component';
@Component({
selector: 'app-cleanup',
templateUrl: './cleanup.component.html',
styleUrls: ['./cleanup.component.css'],
providers: [OrganisationsApiService,
NgbModal,
HttpClientModule,
HttpClient,
]
})
export class CleanupComponent implements OnInit {
submitted: boolean;
loading: boolean;
deleted: number;
constructor(
private http: HttpClient,
private api: OrganisationsApiService,
private modalService: NgbModal) {
}
app.module.ts提供程序
providers: [AwsApiService,
OrganisationsApiService,
LabsApiService,
UsersApiService,
UserManagementDataService,
ConfigDataService,
ErrorDataService,
DeploymentResponseDataService,
ModalOptionsComponent,
ModalConfigurationComponent,
ModalUndeployLabComponent,
ModalCreatedUserComponent,
NgbActiveModal,
HttpClientModule,
HttpClient,
{ provide: HTTP_INTERCEPTORS,
useClass: HttpRequestInterceptor,
multi: true}
],
错误消息:
CleanupComponent.CleanupComponent should createChrome 56.0.2924 (Linux 0.0.0)
<1s
Error: StaticInjectorError(DynamicTestModule)[HttpClient -> HttpHandler]:
StaticInjectorError(Platform: core)[HttpClient -> HttpHandler]:
NullInjectorError: No provider for HttpHandler!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get node_modules/@angular/core/fesm5/core.js:1062:1)
at resolveToken node_modules/@angular/core/fesm5/core.js:1300:1)
at tryResolveToken node_modules/@angular/core/fesm5/core.js:1244:1)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1141:1)
at resolveToken node_modules/@angular/core/fesm5/core.js:1300:1)
at tryResolveToken node_modules/@angular/core/fesm5/core.js:1244:1)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1141:1)
at resolveNgModuleDep node_modules/@angular/core/fesm5/core.js:8369:1)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get node_modules/@angular/core/fesm5/core.js:9057:1)
at resolveDep node_modules/@angular/core/fesm5/core.js:9422:1)
ModalWipedAllComponent.ModalWipedAllComponent should createChrome 56.0.2924 (Linux 0.0.0)
<1s
Error: StaticInjectorError(DynamicTestModule)[HttpClient -> HttpHandler]:
StaticInjectorError(Platform: core)[HttpClient -> HttpHandler]:
NullInjectorError: No provider for HttpHandler!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get node_modules/@angular/core/fesm5/core.js:1062:1)
at resolveToken node_modules/@angular/core/fesm5/core.js:1300:1)
at tryResolveToken node_modules/@angular/core/fesm5/core.js:1244:1)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1141:1)
at resolveToken node_modules/@angular/core/fesm5/core.js:1300:1)
at tryResolveToken node_modules/@angular/core/fesm5/core.js:1244:1)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get node_modules/@angular/core/fesm5/core.js:1141:1)
at resolveNgModuleDep node_modules/@angular/core/fesm5/core.js:8369:1)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get node_modules/@angular/core/fesm5/core.js:9057:1)
at resolveDep node_modules/@angular/core/fesm5/core.js:9422:1)
我希望该项目像在本地一样进行构建,而是获取NullInjectorErrors
更新:component.spec.ts文件 ModalWipedAll
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ModalWipedAllComponent } from './modal-wiped-all.component';
describe('ModalWipedAllComponent', () => {
let component: ModalWipedAllComponent;
let fixture: ComponentFixture<ModalWipedAllComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ModalWipedAllComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ModalWipedAllComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
CleanupComponent
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CleanupComponent } from './cleanup.component';
describe('CleanupComponent', () => {
let component: CleanupComponent;
let fixture: ComponentFixture<CleanupComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CleanupComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CleanupComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
答案 0 :(得分:1)
完成此操作的步骤很少-
导入HttpClientModule
,如下所示
从'@ angular / common / http'导入{HttpClientModule};
并按如下所示使用imports
的{{1}}数组。
@NgModule
HttpClientModule应该在BrowserModule之后添加。此顺序在@NgModule({
...
imports : [BrowserModule,HttpClientModule],
providers: [.... other services ..]
})
数组中很重要。
从组件中删除所有imports
的导入。如果要在根模块上导入它,则足够了。
从app.module.ts中的HttpClientModule
的{{1}}数组中删除HttpClient
,这不是必需的。您可以直接在组件的构造函数中注入HttpClient实例,而无需在提供程序中添加它。
如果您使用的是providers
,则只需要像下面在app.module.ts中导入它-
@NgModule
然后像下面一样使用它
ng-bootstrap
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
不需要添加到提供程序中。
更新:您还需要在规范文件中导入@NgModule({
...
imports : [BrowserModule,HttpClientModule,NgbModule.forRoot()],
providers: [.... other services ..]
})
模块以进行单元测试。 Testbed创建一个用于测试组件的测试模块。将OrganisationsApiService注入组件的构造函数后,需要将其添加到测试平台模块的NgbActiveModal
数组中,如下所示。
这是HttpClientModule
规范文件的代码-
providers
类似地,您需要为CleanupComponent
答案 1 :(得分:0)
如果您正在使用Jasmine和Karma进行ng test进行单元测试,则可能需要在规范文件中导入HttpClientTestingModule。