Search code examples
angularresthttpclient

In Angular 7, where do I find the HttpClient library?


I'm using Angular 7. I have a very simple application so far that just reads a list of books from an endpoint. The src/app/app.component.ts file looks like

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  books;

  constructor(private http: HttpClient) {
    http.get('http://localhost:3000/books.json')
      .subscribe(res => this.books = res.json());
  }
}

However when I start up my app using "ng serve," I get the error

ERROR in src/app/app.component.ts(2,10): error TS2305: Module '"/Users/davea/Documents/workspace/getting_started/home-library/node_modules/@angular/http/http"' has no exported member 'HttpClient'.

Where am I supposed to import my HttpClient library from?


Solution

  • Use the following import import { HttpClient } from '@angular/common/http';.