ionic-native / http发出错误“NullInjectorError:HTTP没有提供者!”

时间:2018-03-19 02:13:49

标签: angular typescript ionic-framework

我使用 ionic 3.2 和angular,对于安装HTTPhttps://ionicframework.com/docs/native/http/)我使用以下命令:

ionic cordova plugin add cordova-plugin-advanced-http
npm install --save @ionic-native/http

在脚本autenticar.ts中,我添加了import { HTTP } from '@ionic-native/http';,如下所示:

import { Component, ViewChild } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';

@Component({
  selector: 'page-autenticar',
  templateUrl: 'autenticar.html'
})
export class AutenticarPage {

  @ViewChild('username') username;
  @ViewChild('password') password;

  constructor(public navCtrl: NavController, public http: HTTP) {
    console.log(http)
  }

...

重新加载应用后,我收到此错误:

Runtime Error
Uncaught (in promise): Error: StaticInjectorError(AppModule)
[AutenticarPage -> HTTP]: StaticInjectorError(Platform: core)[AutenticarPage -> HTTP]:
NullInjectorError: No provider for HTTP!
Error: StaticInjectorError(AppModule)[AutenticarPage -> HTTP]:
StaticInjectorError(Platform: core)[AutenticarPage -> HTTP]:
NullInjectorError: No provider for HTTP! at _NullInjector.get
(http://localhost:8100/build/vendor.js:1376:19) at resolveToken
(http://localhost:8100/build/vendor.js:1674:24) at tryResolveToken
(http://localhost:8100/build/vendor.js:1616:16) at StaticInjector.get
(http://localhost:8100/build/vendor.js:1484:20) at resolveToken
(http://localhost:8100/build/vendor.js:1674:24) at tryResolveToken
(http://localhost:8100/build/vendor.js:1616:16) at StaticInjector.get
(http://localhost:8100/build/vendor.js:1484:20) at resolveNgModuleDep
(http://localhost:8100/build/vendor.js:11228:25) at NgModuleRef_.get
(http://localhost:8100/build/vendor.js:12461:16) at resolveDep
(http://localhost:8100/build/vendor.js:12951:45)

我试试这个answer,说我必须添加app.module.ts这个import { HttpModule } from '@angular/http';,如下所示:

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

....

  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],

...

但仍然是同样的错误。

2 个答案:

答案 0 :(得分:6)

您需要在提供商中添加HTTP

app.module.ts

...

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

...

@NgModule({
  ...

  providers: [
    ...
    HTTP
    ...
  ]
  ...
})
export class AppModule { }

答案 1 :(得分:1)

您需要在 providers

下添加HTTP
providers: [
    HTTP
]