You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic3.2+Angular中ionic-native/http报NullInjectorError: No provider for HTTP!

解决Ionic 3中NullInjectorError: No provider for HTTP!错误

这个NullInjectorError是Angular依赖注入系统找不到HTTP服务的提供者导致的,在Ionic 3中使用@ionic-native插件时,你需要显式注册服务提供者,下面是两种可行的解决方法:

方法1:在AppModule中全局注册(推荐)

这种方式可以让HTTP服务在整个应用中可用,不需要在每个页面单独注册:

  1. 打开你的app.module.ts文件
  2. 导入HTTP模块,然后将其添加到providers数组中:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HTTP } from '@ionic-native/http';
// 其他必要的导入...

@NgModule({
  declarations: [
    // 你的页面组件...
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
    // 其他模块...
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // 你的页面组件...
  ],
  providers: [
    StatusBar,
    SplashScreen,
    HTTP, // 把HTTP添加到这里
    { provide: ErrorHandler, useClass: IonicErrorHandler }
  ]
})
export class AppModule {}

方法2:在单个页面组件中注册

如果你只需要在autenticar页面使用HTTP服务,可以直接在该页面的@Component装饰器中添加providers数组:

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

@Component({ 
  selector: 'page-autenticar', 
  templateUrl: 'autenticar.html',
  providers: [HTTP] // 在这里添加HTTP服务
})
export class AutenticarPage {
  constructor(public navCtrl: NavController, private http: HTTP) {
    // 现在可以正常使用this.http调用插件方法了
  }
}

补充说明

Ionic 3基于Angular 4/5,这一版本的@ionic-native插件还不支持自动提供服务,必须通过上述方式显式注册提供者,这样Angular的依赖注入系统才能找到并实例化HTTP服务,从而解决NullInjectorError问题。

内容的提问来源于stack exchange,提问作者Korvo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:07:55