导入HttpModule错误

时间:2018-04-19 13:55:32

标签: angular angular-http

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

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&#39 ;;时,我没有收到错误。但是弃用说明要将其改为' @ angular / common / http&#39 ;;导致错误的原因是什么?

2 个答案:

答案 0 :(得分:2)

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

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

原因:HttpModule包含在@angular/http包中,不推荐使用

更新1:基于stackblitz

您的导入语句是错误的,因为角度和记录的模块级别更改很少,修改后的导入语句应该看起来像

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

<强> Updated Stackblitz

答案 1 :(得分:0)

您是否已在主模块中导入:

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

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

See here