首页 > 解决方案 > ERROR 错误:StaticInjectorError(AppModule)[UserformService -> HttpClient]:

问题描述

在尝试添加 PrimeNG 表时,我在这里破坏了我的构建:https ://github.com/BillyCharter87/Tech-O-Dex-UI/tree/BrokeIt

我记得将我package.json的从 TypeScript 2.3.4 更新到 2.4.0 并且由于(我认为)我正在使用HeadersHttp我的 POST 调用这一事实而中断。我尝试将其设置回 2.3.4 无济于事。我已经通过添加以下内容修复了我可以解决的问题:

import { HttpClient, HttpHeaders } from "@angular/common/http";

但仍然遇到我现在为HttpClient. 我曾尝试HttpClient像这样导入提供程序:providers: [HttpClient]对于我的 app.module.ts。

完整的错误如下:

AppComponent.html:9 ERROR Error: StaticInjectorError(AppModule)[HttpClient -> HttpHandler]: 
StaticInjectorError(Platform: core)[HttpClient -> HttpHandler]: 
NullInjectorError: No provider for HttpHandler!

标签: angularangular4-httpclient

解决方案


确保您已导入HttpClientModule而不是直接添加HttpClient到提供程序列表中。

有关更多信息,请参阅https://angular.io/guide/http#setup

HttpClientModule实际HttpClient为您提供。请参阅https://angular.io/api/common/http/HttpClientModule

代码示例:

import { HttpClientModule, /* other http imports */ } from "@angular/common/http";

@NgModule({
    // ...other declarations, providers, entryComponents, etc.
    imports: [
        HttpClientModule,
        // ...some other imports
    ],
})
export class AppModule { }

推荐阅读