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

查看:180
本文介绍了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 ..."

投诉服务生成的。 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();
    }

}

投诉表格。 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天全站免登陆