2017-04-25 3 views
21

私はHttpをインポートするサービスを持っています。私のアプリでそれを使用すると、エラーがスローされます。 "g injectionErrorでHttp!Errorのプロバイダがありません"です。私はアプリをlazyloadingしています。また、プロバイダは "ionic g provider ..."角4とイオン3 HTTPのプロバイダなし

苦情-service.ts CLIによって

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class ComplaintService { 
    private complaints: {subject: string, description: string}[] = []; 

    constructor(public http: Http) { 
     this.http = http; 
     this.complaints = null; 
     console.log('Hello ComplaintService Provider'); 
    } 

    addComplaints(complaint: {subject: string, description: string}) { 
     this.complaints.push(complaint); 
    } 

    getComplaints() { 
     return this.complaints.slice(); 
    } 

} 

苦情-form.ts

import { Component } from '@angular/core'; 
import {Validators, FormBuilder, FormGroup } from '@angular/forms'; 
import { IonicPage, NavController, NavParams } from 'ionic-angular'; 
import {ComplaintService} from '../../providers/complaint-service'; 


@IonicPage() 
@Component({ 
    selector: 'page-complaint-form', 
    templateUrl: 'complaint-form.html', 
    providers: [ComplaintService] 
}) 
export class ComplaintForm { 

} 

任意の提案を生成したのか?

+1

あなたはあなたのモジュール – devqon

+0

@devqonで 'providers'に' Http'を登録する必要があります - 私たちは遅延ロードしながら、それを必要としませんまた? – abhiklpm

+0

IonicのネイティブプラグインからのAngularから** HTTP **への** Http' **の違いに注意してください。後者のために 'no provider error'を得る場合は' ion-native'_から 'app.module.ts'にインポートできる' HTTP' - _を含めてください。 –

答えて

59

あなたのモジュール(/app.module.ts)にHttpModuleを登録する必要があります。

import { HttpModule} from '@angular/http'; 

@NgModule({ 
    imports: [ 
    HttpModule 
    ], 
    declarations: [ AppComponent ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { 
} 
+0

このコードはどこに書きますか? – malhobayyeb

+3

あなたのアプリケーションモジュールクラスが定義されているtsファイルで – devqon

+0

私のプロジェクトでこのコードを 'testIonic/src/app/app.module.ts'に追加して動作します:) –

関連する問題