2017-04-25 5 views
21

Ich habe einen Dienst, der Http importiert, und wenn ich es in meiner App verwende, löst es einen Fehler aus. "Kein Provider für Http! Fehler bei g injectionError". Ich lade die App herunter. Auch wurde der Anbieter durch cli erzeugt "ionic g provider ..."Angular 4 und Ionic 3 Kein Provider für HTTP

Beschwerde-service.ts

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(); 
    } 

} 

Beschwerde-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 { 

} 

Irgendwelche Vorschläge?

+1

Sie haben die 'Http' zum' providers' in Ihrem Modul – devqon

+0

@devqon registrieren - brauchen wir sie während verzögertes Laden ebenfalls? – abhiklpm

+0

Beachten Sie den Unterschied zwischen ** "Http" ** von Angular zu ** "HTTP" ** von Ionics nativen Plugins. Im Falle eines 'no provider error' für den letzteren, schließen Sie' HTTP' - _ ein, das von 'ionic-native'_ importiert werden kann - in Ihre' app.module.ts'. –

Antwort

59

Sie haben die HttpModule zu Ihrem Modul registrieren (/app.module.ts):

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

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

Wo soll ich diesen Code schreiben? – malhobayyeb

+3

In der ts-Datei, wo Ihre App-Modul-Klasse – devqon

+0

definiert ist, füge ich diesen Code in meinem Projekt in: 'testIonic/src/app/app.module.ts' und es funktioniert :) –

Verwandte Themen