【发布时间】:2019-08-14 20:31:12
【问题描述】:
我正在使用 Angular 7。到目前为止,我有一个非常简单的应用程序,它只是从端点读取书籍列表。 src/app/app.component.ts 文件看起来像
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());
}
}
但是,当我使用“ng serve”启动我的应用程序时,我收到了错误
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'.
我应该从哪里导入我的 HttpClient 库?
【问题讨论】:
-
为了使用
HttpClient,您还需要在app.module.ts中导入HttpClientModule。否则会出现提供者错误。检查我的答案
标签: angular rest httpclient