Angular 4和Ionic 3没有HTTP的提供者

abh*_*lpm 24 ionic3 angular

我有一个导入Http的服务,当我在我的应用程序中使用它时会抛出错误." 没有Http的提供者!g injectionError时出错 ".我懒得加载应用程序.提供者也是通过cli" ionic g provider ..." 生成的

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

}
Run Code Online (Sandbox Code Playgroud)

投诉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 {

}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

dev*_*qon 67

您必须注册HttpModule到您的模块(/app.module.ts):

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

@NgModule({
  imports: [
    HttpModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)

  • 在定义了app模块类的ts文件中 (3认同)