gpt4 book ai didi

angular - 没有 Http StaticInjectorError 的提供者

转载 作者:太空狗 更新时间:2023-10-29 16:46:25 25 4
gpt4 key购买 nike

我试图从我的 api 中提取数据,并将其填充到我的 ionic 应用程序中,但是当我进入应该填充数据的页面时它崩溃了。下面是我的两个 .ts 文件:

import { Component } from '@angular/core';

import { NavController, LoadingController } from 'ionic-angular';
import { RestService } from '../../providers/rest-service/rest-service';

@Component({
selector: 'page-all-patients',
templateUrl: 'all-patients.html',
providers: [RestService]
})
export class AllPatientsPage {
data: any;
loading: any;

constructor(public navCtrl: NavController, public restService: RestService, public loadingCtrl: LoadingController) {

this.loading = this.loadingCtrl.create({
content: `
<ion-spinner></ion-spinner>`
});
this.getdata();
}

getdata() {
this.loading.present();
this.restService.getJsonData().subscribe(
result => {
this.data=result.data.children;
console.log("Success: " + this.data);
},
err => {
console.error("Error : " + err);
},
() => {
this.loading.dismiss();
console.log('getData completed');
}
);
}
}

和另一个文件:

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

/*
Generated class for the RestServiceProvider provider.

See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/

@Injectable()
export class RestService {

constructor(public http: Http) {
console.log('Hello RestServiceProvider Provider');
}

getJsonData() {
// return Promise.resolve(this.data);

return this.http.get('url').map(res => res.json());
}

}

我也尝试过使用 HttpModule,但这是一个 fatal error 。报错如下:

Error: Uncaught (in promise): Error: StaticInjectorError[Http]: 
StaticInjectorError[Http]:
NullInjectorError: No provider for Http!
Error: StaticInjectorError[Http]:
StaticInjectorError[Http]:
NullInjectorError: No provider for Http!
at _NullInjector.get (http://lndapp.wpi.edu:8100/build/vendor.js:1276:19)

我不确定为什么没有提供者错误,因为这是通过 ionic 框架创建的提供者

最佳答案

为了在您的应用中使用 Http,您需要将 HttpModule 添加到您的 app.module.ts 中:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ErrorHandler } from '@angular/core';
import { HttpModule } from '@angular/http';
...
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot()
]

编辑

正如下面评论中提到的,HttpModule is deprecated 现在,使用 从 '@angular/common/http' 导入 { HttpClientModule };确保 HttpClientModule 在您的 imports:[] 数组

关于angular - 没有 Http StaticInjectorError 的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47492475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com