网站搜索

angular - Ionic2 - 运行时错误 Uncaught (in promise):错误:没有 Http 的提供者


错误:

运行时错误 未捕获(在 promise 中):错误:没有 Http 提供者

嗯,我确实有一个 HTTP 提供者,所有的都已注册。你知道我可以去哪里查看这个问题吗?

app.module.ts

import {NgModule, ErrorHandler} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {IonicApp, IonicModule, IonicErrorHandler} from 'ionic-angular';
import {MyApp} from './app.component';

import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {GoldServiceProvider} from '../providers/gold-service/gold-service';

// ...

@NgModule({
    declarations: [
        MyApp,
        // ...
    ],
    imports: [
        BrowserModule,
        IonicModule.forRoot(MyApp)
    ],
    bootstrap: [IonicApp],
    entryComponents: [
        MyApp,
        // ...
    ],
    providers: [
        GoldServiceProvider,
        StatusBar,
        SplashScreen,
        {
            provide: ErrorHandler,
            useClass: IonicErrorHandler
        },
    ]
})
export class AppModule {
}

gold-service.ts:我的提供者服务

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

@Injectable()
export class GoldServiceProvider {
    ...    
}

答案#1

提供者默认包含 Http,要在你的应用中使用 Http,你需要将 HttpModule 添加到你的 app.module.ts 中:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ErrorHandler } from '@angular/core';
import { HttpModule } from '@angular/http';
...
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],
...