angular - ionic-native/http 发出错误 “NullInjectorError: No provider for HTTP!”

标签 angular typescript ionic-framework

我正在使用 ionic 3.2 和 angular,用于安装 HTTP ( https://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)
  ],

...

但是还是一样的错误。

最佳答案

需要在provider中添加HTTP

应用程序模块.ts

...

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

...

@NgModule({
  ...

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

关于angular - ionic-native/http 发出错误 “NullInjectorError: No provider for HTTP!”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354881/

相关文章:

Angular 2条件Validators.required?

javascript - 在 TypeScript 中访问从另一个类实例化的对象

Angular2 - 将 ngFor item 作为参数传递到管道中?

typescript - `use` 指令的返回类型是什么?

javascript - 识别用户的 ionic 事件来自 navCtrl.pop()

angular - 如何将 TimeLineMax 导入我的 angular2 组件?

javascript - Typescript 强类型 void 函数

angular - Angular 2 Observables 和 Http 中的“未定义”

ionic-framework - 如何获取两个日期之间的天数 ionic+angularjs

javascript - 如何在输入中初始化用户可编辑的数据?