Ganny
Ganny

Reputation: 63

Module not found: Error: Can't resolve '@angular/http'?

Module not found: Error: Can't resolve '@angular/http' even i used import {HttpClientModule} from '@angular/common/http';

Upvotes: 5

Views: 16169

Answers (3)

Santosh Kamat
Santosh Kamat

Reputation: 11

import { HttpModule } from '@angular/http';- Does not work

Try the below one -

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

...
imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
  ],
...

This worked for me.

Upvotes: 1

Praneet
Praneet

Reputation: 1

useimport { HttpClient, HttpHeaders } from '@angular/common/http';

Upvotes: 0

Ergi Kodra
Ergi Kodra

Reputation: 97

in the root AppModule

import { BrowserModule }    from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    BrowserModule,
    // import HttpClientModule after BrowserModule.
    HttpClientModule,
  ],
  declarations: [
    AppComponent,
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

In your Service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class ConfigService {
    constructor(private http: HttpClient) { }

    getConfig() {
        return this.http.get(this.configUrl);
    }
}

In your Components

showConfig() {
  this.configService.getConfig()
    .subscribe((data: any) => {
        // response...
    });
}

https://angular.io/guide/http


Upvotes: 5

Related Questions