错误:Ionic 的构造函数参数中没有 http 的提供者 [英] Error: No provider for http in constructor parameter with Ionic
问题描述
我正在学习葡萄牙语教程,该教程教授如何构建您的第一个 Ionic 应用程序.
I'm following a tutorial in Portuguese that teaches how to build your first Ionic application.
在使用最新版本安装 npm、node.js、angular、Ionic 和 Cordova 后,我开始编写代码.代码本身真的很简单:
After installing npm, node.js, angular, Ionic and Cordova using last versions I started writing the code. The code itself it's really simple:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public feeds: Array<string>;
private url: string = "https://www.reddit.com/new.json";
constructor(public navCtrl: NavController, public http: Http) {
this.http.get(this.url).map(res => res.json()).subscribe(data => {
this.feeds = data.data.children;
});
}
}`
我基本上在使用 Reddit 的 API JSON,可以使用url"变量中的链接访问它,它应该返回一个巨大的 JSON,对其进行转换并存储在feeds"中.问题是每当我提供这个离子应用程序时,我都会收到这个错误:
I'm basically consuming the Reddit's API JSON, which can be accessed using the link in "url" variable and it's supposed to return a huge JSON, transform it and store in "feeds". The problem is whenever I serve this ionic app I get this error:
我已经尝试解决这个问题,并发现如果我删除构造函数中的所有内容,我仍然会收到错误,但如果我删除 "public http: Http" 这是构造函数错误消失了.
I already tried to figure this problem out and discovered that if I erase everything that's inside the constructor I still get the error but if I erase "public http: Http" which is a parameter for the constructor the error is gone.
寻找解决方案我看到有人建议将提供程序添加到@Component,但教程没有说明这一点.
Looking for solutions I saw someone suggestion to add providers to @Component but the tutorial does not say anything about that.
我已经尝试重新安装所有东西,但我仍然收到错误.
I already tried to reinstall everything but i still get the error.
你必须做什么来重现错误:
- 安装最新版本的 npm;
- 安装最新稳定版本的node.js;
- 使用 npm 安装 Angular 最新版本;
- 使用 npm 安装 ionic (2.2.2) 和 cordova (6.5.0) 最新版本;
- 使用以下命令创建应用程序:
ionic start MyReader blank --v2 --appname "BestReader Ever" --id "com.tableless.myreader"
- 将 MyReader/src/pages/home/home.ts 内容更改为上述脚本.
另外,如果您想看一下教程本身,链接是:https://tableless.com.br/criando-uma-aplicacao-movel-com-ionic-2-e-angular-2-em-dez-passos/
Also if you do like to take a look at the tutorial itself, the link is: https://tableless.com.br/criando-uma-aplicacao-movel-com-ionic-2-e-angular-2-em-dez-passos/
推荐答案
使用下面的代码
import {HTTP_PROVIDERS} from '@angular/http';
provider : [ HTTP_PROVIDERS, ....
]
自 angular4 以来,上述答案已被弃用.但是正如我在评论中所说,
The above answer was deprecated since angular4. However As I said in the comment,
import { HttpModule } from '@angular/http';
imports: [ HttpModule ]
这篇关于错误:Ionic 的构造函数参数中没有 http 的提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!