Ionic 2找不到模块“@ angular / common / http / src / headers”

时间:2018-02-11 18:50:13

标签: ionic-framework ionic2 angular2-services

我刚刚开始学习离子,并且在实施休息服务时我遇到了以下错误,

Error: Cannot find module "@angular/common/http/src/headers"
    at Object.199 (http://localhost:8100/build/main.js:87:7)
    at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
    at Object.198 (http://localhost:8100/build/main.js:43:95)
    at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
    at Object.278 (http://localhost:8100/build/main.js:231:75)
    at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
    at Object.222 (http://localhost:8100/build/main.js:166:73)
    at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
    at Object.200 (http://localhost:8100/build/main.js:146:70)
    at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)

我的版本详情是,

Ionic Framwork: 3.9.2
Ionic App scripts :3.1.8
Angular Core:5.0.3
Node:6.9.4
Os Platform: windows 7

我的package.json包含 -

"dependencies": {
    "@angular/common": "5.0.3",
    "@angular/compiler": "5.0.3",
    "@angular/compiler-cli": "5.0.3",
    "@angular/core": "5.0.3",
    "@angular/forms": "5.0.3",
    "@angular/http": "5.0.3",
    "@angular/platform-browser": "5.0.3",
    "@angular/platform-browser-dynamic": "5.0.3",
    "@ionic-native/core": "4.4.0",
    "@ionic-native/http": "^4.5.3",
    "@ionic-native/splash-screen": "4.4.0",
    "@ionic-native/status-bar": "4.4.0",
    "@ionic/storage": "2.1.3",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "rxjs": "5.5.2",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.18"
  },

“devDependencies”:{     “@ ionic / app-scripts”:“3.1.8”,     “打字稿”:“2.4.2”   } app.module看起来像这样,

import { BrowserModule } from '@angular/platform-browser';
import {HttpClientModule} from '@angular/common/http';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { Http,HttpModule } from '@angular/http';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { RestServiceProvider } from '../providers/rest-service/rest-service';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    RestServiceProvider
  ]
})
export class AppModule {}

最后一个我的服务看起来像这样,

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http/src/headers';
/*
  Generated class for the RestServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class RestServiceProvider {
  username:string;
  password:string;
  constructor(public http: HttpClient) {
    console.log('Hello RestServiceProvider Provider');
  }
  load(){

    let requestHeader=new HttpHeaders();
    requestHeader.append("Content-Type","application/json");
    let postParams={
      UserName:this.username,
      Password:this.password
    }
    return new Promise(resolve=>{
      this.http.post("htttp/someservice", postParams,{headers:requestHeader})
      .subscribe(data => {
        alert(data);
      }, error => {
        console.log(error);// Error getting the data
      });
    });
  }
}
寻求帮助。 : - )

1 个答案:

答案 0 :(得分:2)

更改 import { HttpHeaders } from '@angular/common/http/src/headers';import { HttpHeaders } from '@angular/common/http';