以首页列表为例:
indexLists()----是自定义的一个方法
uni-request()—是请求的数据的函数
url------接的是请求的地址
success(res){}—返回成功典范参数
res.data----后台显示的数据
indexLists(){
uni.request({
url:'http://192.168.0.2:851/api/index/index',
success:(res) => {
console.log(res.data);
if(res.statusCode ==200){
let list = this.setTime(res.data.data.list);//先保存json数据
this.indexList = this.reload ? list : this.indexList.concat(list);
this.last_id = list[list.length - 1].id;
this.reload = false;
}
},
fail: (data, code) => {
console.log('fail' + JSON.stringify(data));
},
complete: () => {
uni.stopPullDownRefresh();
}
})
},
setTime: function(items) {
var newItems = [];
items.forEach(e => {
newItems.push({
id: e.id,
title: e.title,
logo: e.logo,
min_order: e.min_order,
price: e.price
});
});
return newItems;
}