uniapp 学习笔记十五 开启下拉刷新加载和封装请求方法
cake.vue
<template>
<view>
<view class="flex justify-between padding">
<view class="">
猫屎咖啡
</view>
<view class="">
牛奶拿铁
</view>
</view>
<view class="cont">
<good-item v-for="(item,index) in glist" :gdata="item"></good-item>
</view>
</view>
</template>
<script>
export default {
data() {
return {
glist:[],
page:0
}
},
methods: {
handleDetail(idx){
console.log(idx);
uni.navigateTo({
url:'../detail/detail?idx='+idx
})
},
loadData(){
let skip = this.page * 8
let url = `/1.1/classes/goods?where{"bcid":1}&limit=8&skip=${skip}`
this.$get(url).then(res=>{
uni.stopPullDownRefresh()
let {results} = res
if (results.length) {
this.page++
this.glist = [
...this.glist,
...res.results
]
return
}
uni.showToast({
title:'这回真没了...',
icon:'none'
})
})
}
},
onLoad() {
this.loadData()
},
onReachBottom() {
console.log('触底了');
this.loadData()
},
onPullDownRefresh() {
this.glist = []
this.page = 0
this.loadData()
}
}
</script>
<style lang="scss">
.cont{
display: flex;
flex-wrap: wrap;
padding: 15upx;
justify-content: space-between;
}
</style>
page.json
{
"path" : "pages/cake/cake",
"style" :
{
"navigationBarTitleText": "",
"enablePullDownRefresh": true //开启
}
},