2017-07-28 113 views
1

我收到一个错误,我不知道如何解决。我已经在寻找解决方案,但他们没有帮助我。Http provider Error Ionic 3

这就是使用Ionic Native Http进行HTTP请求,GET和POST。

的错误:它看起来像我没有设置HTTP提供,但在app.module.ts进口

ERROR Error: Uncaught (in promise): Error: No provider for HTTP! 
    Error: No provider for HTTP! 
     at injectionError (vendor.js:1590) 
     at noProviderError (vendor.js:1628) 
     at ReflectiveInjector_._throwOrNull (vendor.js:3129) 
     at ReflectiveInjector_._getByKeyDefault (vendor.js:3168) 
     at ReflectiveInjector_._getByKey (vendor.js:3100) 
     at ReflectiveInjector_.get (vendor.js:2969) 
     at AppModuleInjector.get (ng:///AppModule/module.ngfactory.js:240) 
     at AppModuleInjector.getInternal (ng:///AppModule/module.ngfactory.js:365) 
     at AppModuleInjector.NgModuleInjector.get (vendor.js:3936) 
     at resolveDep (vendor.js:11398) 
     at injectionError (vendor.js:1590) 
     at noProviderError (vendor.js:1628) 
     at ReflectiveInjector_._throwOrNull (vendor.js:3129) 
     at ReflectiveInjector_._getByKeyDefault (vendor.js:3168) 
     at ReflectiveInjector_._getByKey (vendor.js:3100) 
     at ReflectiveInjector_.get (vendor.js:2969) 
     at AppModuleInjector.get (ng:///AppModule/module.ngfactory.js:240) 
     at AppModuleInjector.getInternal (ng:///AppModule/module.ngfactory.js:365) 
     at AppModuleInjector.NgModuleInjector.get (vendor.js:3936) 
     at resolveDep (vendor.js:11398) 
     at c (polyfills.js:3) 
     at Object.reject (polyfills.js:3) 
     at NavControllerBase._fireError (vendor.js:43003) 
     at NavControllerBase._failed (vendor.js:42991) 
     at vendor.js:43046 
     at t.invoke (polyfills.js:3) 
     at Object.onInvoke (vendor.js:4508) 
     at t.invoke (polyfills.js:3) 
     at r.run (polyfills.js:3) 
     at polyfills.js:3 

代码:

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { ErrorHandler, NgModule } from '@angular/core'; 
import { HttpModule } from '@angular/http'; 
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; 

import { MyApp } from './app.component'; 
import { HomePage } from '../pages/home/home'; 
import { ListPage } from '../pages/list/list'; 
import { CadastroPage } from '../pages/cadastro/cadastro'; 

import { ApiService } from '../services/api.service'; 

import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 

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

api.service.ts

import { Injectable } from '@angular/core'; 
import { HTTP } from '@ionic-native/http'; 

@Injectable() 
export class ApiService { 

    constructor (private http: HTTP) { } 

    private apiUrl = '<VALID_URL>'; 

    get() { 
     // Some OK http request here! 
    } 
} 

packaje.json

{ 
     "name": "app", 
     "version": "0.0.1", 
     "author": "Ionic Framework", 
     "homepage": "http://ionicframework.com/", 
     "private": true, 
     "scripts": { 
     "clean": "ionic-app-scripts clean", 
     "build": "ionic-app-scripts build", 
     "lint": "ionic-app-scripts lint", 
     "ionic:build": "ionic-app-scripts build", 
     "ionic:serve": "ionic-app-scripts serve" 
    }, 
     "dependencies": { 
     "@angular/common": "4.1.3", 
     "@angular/compiler": "4.1.3", 
     "@angular/compiler-cli": "4.1.3", 
     "@angular/core": "4.1.3", 
     "@angular/forms": "4.1.3", 
     "@angular/http": "4.1.3", 
     "@angular/platform-browser": "4.1.3", 
     "@angular/platform-browser-dynamic": "4.1.3", 
     "@ionic-native/core": "3.12.1", 
     "@ionic-native/http": "^4.1.0", 
     "@ionic-native/splash-screen": "3.12.1", 
     "@ionic-native/status-bar": "3.12.1", 
     "@ionic/storage": "2.0.1", 
     "cordova-android": "^6.2.3", 
     "cordova-plugin-console": "1.0.5", 
     "cordova-plugin-device": "1.1.4", 
     "cordova-plugin-splashscreen": "~4.0.1", 
     "cordova-plugin-statusbar": "2.2.2", 
     "cordova-plugin-whitelist": "1.3.1", 
     "ionic-angular": "3.5.3", 
     "ionic-plugin-keyboard": "~2.2.1", 
     "ionicons": "3.0.0", 
     "rxjs": "5.4.0", 
     "sw-toolbox": "3.6.0", 
     "zone.js": "0.8.12" 
    }, 
     "devDependencies": { 
     "@ionic/app-scripts": "2.0.2", 
     "@ionic/cli-plugin-cordova": "1.5.0", 
     "@ionic/cli-plugin-ionic-angular": "1.4.0", 
     "ionic": "3.6.0", 
     "typescript": "2.3.4" 
    }, 
     "description": "An Ionic project", 
     "cordova": { 
      "plugins": { 
       "cordova-plugin-console": {}, 
       "cordova-plugin-device": {}, 
       "cordova-plugin-splashscreen": {}, 
       "cordova-plugin-statusbar": {}, 
       "cordova-plugin-whitelist": {}, 
       "ionic-plugin-keyboard": {} 
      }, 
      "platforms": [ 
       "android" 
      ] 
     } 
    } 

回答

2

在你APIService为此

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 

@Injectable() 
export class ApiService { 

    constructor (private http: Http) { } 

    private apiUrl = '<VALID_URL>'; 

    get() { 
     // Some OK http request here! 
    } 
} 

你的错误很可能来源于此行

import { HTTP } from '@ionic-native/http'; 

但是您从@angular/http导入了模块!

+0

这是否解决了您的问题? – brijmcq

+0

Thaks @brijmcq! –

1

我有同样的错误,我添加模块providers阵列像这样:

... 

import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { HTTP } from '@ionic-native/http'; 

... 

providers: [ 
    StatusBar, 
    SplashScreen, 
    HTTP, 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 
    ] 

... 

希望这将有助于

0

我有ionic3同样的错误。要解决此错误,请在提供商中添加HTTP

import { HTTP } from '@ionic-native/http'; 

... 

providers: [ 
    StatusBar, 
    SplashScreen, 
    HTTP, 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 
]