[英]Angular 4 and Ionic 3 No provider for HTTP
I have a service which imports Http, and when I use it in my app it throws error. 我有一个导入Http的服务,当我在我的应用程序中使用它时会抛出错误。 " No provider for Http! Error at g injectionError ".
“ 没有Http的提供者!g injectionError时出错 ”。 I am lazyloading the app.
我懒得加载应用程序。 Also the provider was generated through cli "
ionic g provider ...
" 提供者也是通过cli“
ionic g provider ...
”生成的ionic g provider ...
complaint-service.ts 投诉service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ComplaintService {
private complaints: {subject: string, description: string}[] = [];
constructor(public http: Http) {
this.http = http;
this.complaints = null;
console.log('Hello ComplaintService Provider');
}
addComplaints(complaint: {subject: string, description: string}) {
this.complaints.push(complaint);
}
getComplaints() {
return this.complaints.slice();
}
}
complaint-form.ts 投诉form.ts
import { Component } from '@angular/core';
import {Validators, FormBuilder, FormGroup } from '@angular/forms';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {ComplaintService} from '../../providers/complaint-service';
@IonicPage()
@Component({
selector: 'page-complaint-form',
templateUrl: 'complaint-form.html',
providers: [ComplaintService]
})
export class ComplaintForm {
}
Any suggestions? 有什么建议么?
You have to register the HttpModule
to your module ( /app.module.ts
): 您必须将
HttpModule
注册到您的模块( /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. 将
HTTP
添加到提供者列表中。 (to AppModule
) (到
AppModule
)
import { HTTP } from '@ionic-native/http';
And 和
providers: [
...,
HTTP
]
Working with Angulat5+, Ionic 3+ 使用Angulat5 +,Ionic 3+
HttpModule is deprecated in the latest version of ionic3 and Angular5. HttpModule在最新版本的ionic3和Angular5中已被弃用。 So, you have to use HttpClientModule instead of HttpModule.
因此,您必须使用HttpClientModule而不是HttpModule。 Other things will be same as @devqon's answer.
其他事情与@ devqon的答案相同。 After that, you can use HttpClient in your service or component file.
之后,您可以在服务或组件文件中使用HttpClient。
For more info, please refer to the original document of Angular. 有关详细信息,请参阅Angular的原始文档。
https://angular.io/guide/http 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 不要忘记使用离子cordova运行浏览器,因为你需要一个平台来运行cordova插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.