嗨,我正在尝试做一个简单的Http获取请求,但无法使它在离子v2 Beta.
这是我的app.js:
import {App, Platform} from 'ionic-angular';
import {TabsPage} from './pages/tabs/tabs';
import {HTTP_BINDINGS} from 'angular2/http';
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [HTTP_BINDINGS],
config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
static get parameters() {
return [[Platform]];
}
constructor(platform) {
this.rootPage = TabsPage;
platform.ready().then(() => {
});
}
}这是我的页面1.js:
import {Page} from 'ionic-angular';
import {Http} from 'angular2/http';
@Page({
templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 {
constructor(http:Http) {
this.mget = http.get("https://httpbin.org/ip")
.subscribe(data => {
var alert = Alert.create({
title: "Your IP Address",
subTitle: data.json().origin,
buttons: ["close"]
});
this.nav.present(alert);
}, error => {
console.log(JSON.stringify(error.json()));
});
}
}当将http:Http添加到构造函数->构造函数(Http:Http)时,整个应用程序在浏览器中变为空白.我在控制台上看到了一个错误:
错误:找不到模块“./页1/页1”
我也在Page1.js中尝试过这种方法:
export class Page1 {
constructor() {
}
makeGetRequest() {
this.http.get("https://httpbin.org/ip")
.subscribe(data => {
var alert = Alert.create({
title: "Your IP Address",
subTitle: data.json().origin,
buttons: ["close"]
});
this.nav.present(alert);
}, error => {
console.log(JSON.stringify(error.json()));
console.log('yolo')
alert('hello');
});
}
}然后在page1.html中(单击)调用makeGetRequest(),但它返回以下命令:
异常:“单击”计算过程中的错误
原始异常: TypeError: this.http是未定义的
请帮助!:)
这是解决方案:
第1.js页:
import {Page} from 'ionic-angular';
import {Http} from 'angular2/http';
@Page({
templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 {
static get parameters(){
return [Http];
}
constructor(http) {
this.http = http;
this.mget = this.http.get("https://httpbin.org/ip")
.subscribe(data => {
console.log(data);
}, error => {
console.log('faild');
});
}
}app.js:
import {App, Platform} from 'ionic-angular';
import {TabsPage} from './pages/tabs/tabs';
import { HTTP_PROVIDERS } from 'angular2/http';
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [HTTP_PROVIDERS],
config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
static get parameters() {
return [[Platform]];
}
constructor(platform) {
this.rootPage = TabsPage;
platform.ready().then(() => {
});
}
}发布于 2016-03-02 21:20:52
请尝尝这个
export class Page1 {
static get parameters(){
return [Http];
}
constructor(http) {
this.http = http;
this.mget = this.http.get("https://httpbin.org/ip")
.subscribe(data => {
var alert = Alert.create({
title: "Your IP Address",
subTitle: data.json().origin,
buttons: ["close"]
});
this.nav.present(alert);
}, error => {
console.log(JSON.stringify(error.json()));
});
}
}我建议您在单独的服务中编写get请求,并将其插入到页面中。
还请看一下这个- http://tphangout.com/?p=113
详细和简单的说明是给出了一个从一个Ionic 2应用程序简单的GET请求。
发布于 2016-03-02 20:56:06
我相信你需要在你的app.js中加入HTTP_BINDINGS而不是HTTP_BINDINGS,把providers: [HTTP_BINDINGS]改为providers: [HTTP_PROVIDERS]
请参阅Angular2文档
https://stackoverflow.com/questions/35757474
复制相似问题