【发布时间】:2018-12-27 15:44:22
【问题描述】:
我是一个基于 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 文件 模态擦除全部
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();
});
});
清理组件
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();
});
});
【问题讨论】:
-
你不必提供HttpClient,只要HttpClientModule就够了。此外,您不需要在每个组件中单独提供。如果您已在 root 中提供,它将可用于完整的应用程序
-
我在各处都提供了 HttpClient 模块,试图修复错误。它在根模块 app.module.ts 中提供,并在同一个 app.module.ts 文件中正确导入。然而错误仍然存在。
-
在提供任何服务之前提供 HttpClientModule。顺序很重要。最好的位置是把它放在 app.module.ts 的导入数组中 BrowserModule 之后
-
@CallumMartin 检查我的答案。我已经给出了步骤。
-
现在注意到了.. 您已将 HttpClientModule 放在提供程序中。你不必那样做。你必须把它放在
imports数组中。