Angular 4 和 Ionic 3 没有 HTTP 提供者 [英] Angular 4 and Ionic 3 No provider for HTTP

查看:15
本文介绍了Angular 4 和 Ionic 3 没有 HTTP 提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导入 Http 的服务,当我在我的应用程序中使用它时,它会引发错误.没有 Http 提供程序!g injectionError 时出错".我正在延迟加载应用程序.提供程序也是通过 cli "ionic g provider ..."

I have a service which imports Http, and when I use it in my app it throws error. "No provider for Http! Error at g injectionError". I am lazyloading the app. Also the provider was generated through cli "ionic g provider ..."

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

}

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

}

有什么建议吗?

推荐答案

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

You have to register the HttpModule to your module (/app.module.ts):

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

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

这篇关于Angular 4 和 Ionic 3 没有 HTTP 提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆