Javascript Nativescript:如何将HTTP值存储到公共变量
我在这里遇到了一些问题,我混合使用Angular2和Nativescript的原生JS来使用HTTP,因为在这个实现之前,我在Angular2 HTTP方面遇到了问题,但是我对如何将HTTP请求的JSON响应放到monthList变量中,以便可以到处读取感到沮丧。请帮忙,谢谢 这是我的密码:Javascript Nativescript:如何将HTTP值存储到公共变量,javascript,android,angular,nativescript,angular2-nativescript,Javascript,Android,Angular,Nativescript,Angular2 Nativescript,我在这里遇到了一些问题,我混合使用Angular2和Nativescript的原生JS来使用HTTP,因为在这个实现之前,我在Angular2 HTTP方面遇到了问题,但是我对如何将HTTP请求的JSON响应放到monthList变量中,以便可以到处读取感到沮丧。请帮忙,谢谢 这是我的密码: public monthList = []; constructor(location: Location, private page: Page, private router: Router
public monthList = [];
constructor(location: Location, private page: Page, private router: Router, private testing: any) {
this.router = router;
this.testing = testing;
let http = require("http");
http.getJSON("link")
.then(function (r) {
// Argument (r) is JSON!
}, function (e) {
// Argument (e) is Error!
console.log(e);
});
}
你遇到了什么特别的问题吗?我认为这很简单:
class SomeAngularClass {
constructor() {
http.getJSON("link")
.then(function(r) {
this.monthList.push(r);
}, function (e) {
console.log(e);
});
}
}
然后您可以通过以下方式访问:
SomeAngularClass().monthList;
然后将返回对象设置为月份列表
this.monthList = r;
尝试执行console.dump(r)并查看返回的内容。您可能必须执行类似r.xxx的操作,或者按照其结构进行操作
然后你可以做一些类似的事情
this.monthList.month
this.monthList[0].month.
并访问当月数据等。或者你可能需要做一些类似的事情
this.monthList.month
this.monthList[0].month.
例如,在我的项目中,我有一个嵌套的JSON对象返回,我需要将其设置为类似于r.value.data的值。稍后将尝试它…但我想我以前尝试过,但没有成功…只是一个提醒…这是nativescript的angular2中的一个要求…:)大部分控制台返回未捕获的承诺类型错误…然后在android emulator中仅显示空白…