目录
?首先: 封装一个request请求的js文件,用的是Promise
?
?
<!--pages/seven/seven.wxml-->
<navigation-bar title="旅途" back="{{false}}" color="black" background="#FFF"></navigation-bar>
<scroll-view scroll-y="{{true}}" style="width:100%;height:{{windowHeight}}px;" bindscrolltolower="onReachBottom">
<view>
<view>
<block wx:for="{{houselist}}" wx:key="item.data.houseId">
<view>{{item.data.houseName}}</view>
<image src="{{item.data.image.url}}"></image>
</block>
</view>
</view>
</scroll-view>
// pages/seven/seven.js
import { myRequest } from "../../services/request/index"
const systemInfo = wx.getSystemInfoSync();
const windowHeight = systemInfo.windowHeight;
Page({
/**
* 页面的初始数据
*/
data: {
windowHeight:windowHeight,
houselist:[],
allCities:{},
currentPage:1
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.getCityData(),
this.getHouselistData()
},
async getCityData(){
const cityRes = await myRequest({
url:"http://codercba.com:1888/api/city/all"
})
this.setData({allCities:cityRes})
},
async getHouselistData(){
const houseRes = await myRequest({
url:"http://codercba.com:1888/api/home/houselist",
data:{
page:this.data.currentPage
}
})
console.log(this.data.houselist)
console.log(houseRes.data)
const finalHouseList = [...this.data.houselist,...houseRes.data]
this.setData({houselist:finalHouseList})
this.data.currentPage++
},
onReachBottom(){
this.getHouselistData()
console.log("触底了")
}
})
/* pages/seven/seven.wxss */
{
"usingComponents": { "navigation-bar": "/components/navigation-bar/navigation-bar"}
}
export function myRequest(option){
return new Promise((resolve,reject)=>{
wx.request({
...option,
success:(res)=>{
resolve(res.data)
},
fail:reject
})
})
}