You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular2调用Web API报错No provider for Http,求解决方案

Fixing "No provider for Http" Error in Angular 2

Looks like you've hit a super common Angular 2 gotcha! The issue here is that even though you're injecting Http into your service class, Angular doesn't provide this service by default—you need to explicitly import the module that registers all the necessary providers for it.

Here's how to fix it step by step:

  1. Open your Module.ts file and add the import for HttpModule from @angular/http:
import { HttpModule } from '@angular/http';
  1. Add HttpModule to the imports array in your @NgModule decorator. This tells Angular to set up all the dependencies needed for the HTTP service to work:
@NgModule({
  imports: [BrowserModule, ReactiveFormsModule, appRouterModule, HttpModule], // Add HttpModule here
  declarations: [AppComponent, LoginComponent, RegistrationComponent],
  bootstrap: [AppComponent],
  providers: [services]
})
export class AppModule { }

Quick extra tips:

  • If you're working with Angular 4.3 or later, the official recommendation is to use the newer HttpClientModule (from @angular/common/http) instead of the old HttpModule. It comes with nicer features like automatic JSON parsing and better type support—worth checking out if your project version allows it!
  • A small style note: TypeScript class names usually start with a capital letter, so renaming services to Services would make your code more consistent with standard conventions.

Once you make these changes, restart your Angular app and the "No provider for Http" error should disappear entirely. Your login API call should now execute without issues!

内容的提问来源于stack exchange,提问作者Hardik Gondalia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:45:27