导入 HttpModule 错误

Import HttpModule Error

在我的组件中,我导入了以下内容

import { Http, Response, URLSearchParams, HttpClient, HttpHeaders  } from '@angular/common/http';

但我收到错误

ERROR in src/app/app.module.ts(35,9): error TS2305: Module '"/Users/strypeez/Dropbox/gosco-webNEW/gosco-webUPTODATE/node_modules/@angular/common/http"' has no exported member 'HttpModule'.
src/app/ownership-cost/ownership-form.component.ts(8,10): error TS2305: Module '"/Users/strypeez/Dropbox/gosco-webNEW/gosco-webUPTODATE/node_modules/@angular/common/http"' has no exported member 'Http'.
src/app/ownership-cost/ownership-form.component.ts(8,16): error TS2305: Module '"/Users/strypeez/Dropbox/gosco-webNEW/gosco-webUPTODATE/node_modules/@angular/common/http"' has no exported member 'Response'.
src/app/ownership-cost/ownership-form.component.ts(8,26): error TS2305: Module '"/Users/strypeez/Dropbox/gosco-webNEW/gosco-webUPTODATE/node_modules/@angular/common/http"' has no exported member 'URLSearchParams'. 

我在使用“@angular/http”时没有收到错误消息;但弃用说明说将其更改为“@angular/common/http”;是什么导致了错误?

您应该使用 @angular/common/http 中的 HttpClientModule 并使用以下语句

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

原因:HttpModule@angular/http 包中可用,已弃用

更新 1:基于 stackblitz

您的导入语句是错误的,因为 angular 并记录了很少的模块级更改,修改后的导入语句应该类似于

import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { Http, Response, URLSearchParams } from '@angular/http';

Updated Stackblitz

你有没有导入你的主模块:

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

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

See here