Angular 4 and Ionic 3 No provider for HTTP

北战南征 提交于 2019-11-29 02:57:52
devqon

You have to register the HttpModule to your module (/app.module.ts):

import { HttpModule} from '@angular/http';

@NgModule({
  imports: [
    HttpModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule {
}

Add HTTP to the providers list as well. (to AppModule)

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

And

  providers: [
    ...,
    HTTP 
  ]

Working with Angulat5+, Ionic 3+

HttpModule is deprecated in the latest version of ionic3 and Angular5. So, you have to use HttpClientModule instead of HttpModule. Other things will be same as @devqon's answer. After that, you can use HttpClient in your service or component file.

For more info, please refer to the original document of Angular.

https://angular.io/guide/http

import {HttpClientModule} from "@angular/common/http";

@NgModule({
  imports: [
    HttpClientModule
 ],
 declarations: [ AppComponent ],
 bootstrap:    [ AppComponent ]
})
export class AppModule {
}
import {HttpClientModule} from "@angular/common/http";

@NgModule({
  declarations: [
    ChatPage,
  ],
  imports: [
    IonicPageModule.forChild(ChatPage),
    HttpModule,
    HttpClientModule,// add HttpClientModule to import
    SocketIoModule.forRoot(config)
  ],

})

Dont forget to run with ionic cordova run browser because you need a platform to run cordova plugins

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!