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

后端 未结 9 1361
被撕碎了的回忆
被撕碎了的回忆 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

    0 讨论(0)
  • 2020-12-01 04:39

    You should import http from @angular/http in your service:

    import {Http} from '@angular/http';
    
    constructor(private http: http) {} // <--Then Inject it here
    
    
    // now you can use it in any function eg:
    getUsers() {
        return this.http.get('whateverURL');
    }
    
    0 讨论(0)
  • 2020-12-01 04:43

    note: This is for @angular/http, not the asked @angular/common/http!

    Just import in this way, WORKS perfectly:

    // Import HttpModule from @angular/http
    import {HttpModule} from '@angular/http';
    
    
    @NgModule({
      declarations: [
        MyApp,
        HelloIonicPage,
        ItemDetailsPage,
        ListPage
      ],
      imports: [
        BrowserModule,
        HttpModule,
        IonicModule.forRoot(MyApp),
      ],
      bootstrap: [...],
      entryComponents: [...],
      providers: [... ]
    })
    

    and then you contruct in the service.ts like this:

    constructor(private http: Http) { }
    
    getmyClass(): Promise<myClass[]> {
      return this.http.get(URL)
                 .toPromise()
                 .then(response => response.json().data as myClass[])
                 .catch(this.handleError);
    }
    
    0 讨论(0)
  • 2020-12-01 04:44

    For anyone using Ionic 3 and Angular 5, I had the same error pop up and I didn't find any solutions here. But I did find some steps that worked for me.

    Steps to reproduce:

    • npm install -g cordova ionic
    • ionic start myApp tabs
    • cd myApp
    • cd node_modules/angular/common (no http module exists).

    ionic:(run ionic info from a terminal/cmd prompt), check versions and make sure they're up to date. You can also check the angular versions and packages in the package.json folder in your project.

    I checked my dependencies and packages and installed cordova. Restarted atom and the error went away. Hope this helps!

    0 讨论(0)
  • 2020-12-01 04:45

    Beware of auto imports. my HTTP_INTERCEPTORS was auto imported like this:

    import { HTTP_INTERCEPTORS } from '@angular/common/http/src/interceptor';
    

    instead of

    import { HTTP_INTERCEPTORS } from '@angular/common/http';
    

    which caused this error

    0 讨论(0)
  • 2020-12-01 04:49

    I was using http in angular 5 that was a problem. Using Httpclient resolved the issue.

    0 讨论(0)
提交回复
热议问题