【问题标题】:In Angular 7, where do I find the HttpClient library?在 Angular 7 中,我在哪里可以找到 HttpClient 库?
【发布时间】: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


【解决方案1】:

使用以下导入import { HttpClient } from '@angular/common/http';.

【讨论】:

    【解决方案2】:

    HttpClient 库可以从import { HttpClient } from '@angular/common/http'; 导入。

    【讨论】:

      【解决方案3】:
      1. 您需要从@angular/common/http 导入app.module.ts 中的HttpClientModule,如下所示,才能使用HttpClient

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

      2. 然后在app.module.ts中的@NgModuleimports数组中包含HttpClientModule

      3. 然后在您的服务类中,您需要像下面这样导入HttpClient -

        import {HttpClient} from '@angular/common/http'

      较旧的Http client/HttpModule 在最新版本的 Angular 中已被删除。

      【讨论】:

        【解决方案4】:

        我被阻止了和你一样的错误。

        ERROR in node_modules/@angular/http/src/http_module.d.ts(11,10): error TS2305: Module '"C:/PROIECTE/SfaBackend-Angular/Taswik.Core/ClientApp/node_modules/@angular/http/src/http"' has no exported member 'HttpClient'.
        

        我做了所有的改变,但他们没有工作,不是在改变之前 node_modules/@angular/http/src/http.d.ts/HttpClientHttpHttpClient 运行 ng serve

        我希望它也对你有用。

        【讨论】:

          猜你喜欢
          • 2013-10-17
          • 1970-01-01
          • 2013-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-17
          • 2018-01-18
          • 2014-07-16
          相关资源
          最近更新 更多