Cannot find the '@angular/common/http' module

后端 未结 9 1393
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 04:25

I am following this fundamental tutorial on Angular about Http.

As one can see in the \"Setup: Installing the module\" section, they import the HttpClientModule as f

9条回答
  •  臣服心动
    2020-12-01 04:39

    Important: HttpClientModule is for Angular 4.3.0 and above. Check @Maximus' comments and @Pengyy's answer for more info.


    Original answer:

    You need to inject HttpClient in your component/service not the module. If you import HttpClientModule in your @NgModule

    // app.module.ts:
     
    import {NgModule} from '@angular/core';
    import {BrowserModule} from '@angular/platform-browser';
     
    // Import HttpClientModule from @angular/common/http
    import {HttpClientModule} from '@angular/common/http';
     
    @NgModule({
      imports: [
        BrowserModule,
        // Include it under 'imports' in your application module
        // after BrowserModule.
        HttpClientModule,
      ],
    })
    export class MyAppModule {}
    

    So change

    constructor(private httpClient: HttpModule) {}
    

    to

    constructor(private httpClient: HttpClient) {}
    

    as it's been written in the documentation


    However, since you imported the HttpModule

    you need to inject constructor(private httpClient: Http) as @Maximus stated in the comments and @Pengyy in this answer.

    And for more info on differences between HttpModule and HttpClientModule, check this answer

提交回复
热议问题