Search code examples
angular6

Module has no exported member 'http' [2305]


Hello I am trying to import http call from '@angular/common/http' and am getting error message that module has no exported member 'http'

Error:[ts] Module '"e:/car/node_modules/@angular/common/http"' has no exported member 'Http'. [2305]

import {
  RouterModule
} from '@angular/router';
import {
  BrowserModule
} from '@angular/platform-browser';
import {
  NgModule
} from '@angular/core';

import {
  AppRoutingModule
} from './app-routing.module';
import {
  AppComponent
} from './app.component';
import {
  HomeComponent
} from './home/home.component';
import {
  HttpClientModule
} from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    RouterModule.forRoot([{
      path: '',
      component: HomeComponent
    }])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

import {
  Http
} from '@angular/common/http';
import {
  Component,
  OnInit
} from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private http: Http) {}
  products = [];
  fetchData = function() {
    this.http.get('http://localhost:5555/products').subscribe(
      (res: Response) => {
        this.products = res.json();
      }
    );
  };
  ngOnInit() {
    this.fetchData();
  }

}

the below code is used for inserting json file which includes the details


Solution

  • HttpClientModule will inject HttpClient service not the Http.

    Please use HttpClient in constructor injection in the HomeComponent.

    constructor(private http: HttpClient) {}
    

    For Reference created sample stackblitz code.