当前位置:   article > 正文

小程序上拉加载、下拉刷新的使用方法_微信小程序如何设置view页面上拉触顶下拉刷新

微信小程序如何设置view页面上拉触顶下拉刷新

一、定义数据

  1. data() {
  2. return {
  3. list: [], //数据列表
  4. noMore: false, // 全部加载后底部提示
  5. total: 0, // 总条数
  6. data: {
  7. page: 1, // 页码
  8. limit: 10 // 每次请求数量
  9. },
  10. };
  11. },

二、启用下拉刷新 

在页面的 .json 配置文件中,将 enablePullDownRefresh 设置为 true

三、监听下拉刷新 

操作下拉刷新时,设置当前页数、总数、请求列表清零等

再次发起请求,请求完毕停止下拉刷新效果

  1. onPullDownRefresh() {
  2.   this.data.page = 1
  3.   this.total = 0
  4.   this.list = []
  5.   this.record()
  6.   uni.stopPullDownRefresh()
  7. },

四、上拉触底加载

  1. onReachBottom() {
  2. if (this.data.page * this.data.limit >= this.total) {
  3. this.noMore = true
  4. return
  5. }
  6. this.data.page++ // 让页码值自增+1
  7. this.record()
  8. },

五、初次加载页面的操作

首次加载页面请求数据后,进行判断:如果数据请求完毕显示‘没有更多了’

  1. onLoad: function(options) {
  2.   this.recordList();
  3.   if (this.data.page * this.data.limit >= this.total) {
  4.     this.noMore = true
  5.     return
  6.   }
  7. },

六、调接口拿数据

每次请求十条数据,并添加到数据列表

  1. async recordList() {
  2. const res = await uni.$http.get('/api/recordList', this.data)
  3. // console.log(res.data); // 每次请求返回十条数据
  4. if (res.statusCode !== 200) return uni.$showErrorMsg("获取充值记录失败")
  5. this.list = [...this.list, ...res.data.records]
  6. this.total = res.data.total // 通过接口获取总条数
  7. },

七、全部代码

  1. <template>
  2. <view class="content">
  3. <uni-row v-for="(item, index) in list" :key="item.index">
  4. ... ...(需要渲染的数据)
  5. </uni-row>
  6. <view class="bottom" v-if="noMore">
  7. 没有更多了
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. list: [],
  16. noMore: false,
  17. data: {
  18. page: 1,
  19. limit: 12
  20. },
  21. total: 0,
  22. };
  23. },
  24. onLoad: function(options) {
  25. this.recordList();
  26. if (this.data.page * this.data.limit >= this.total) {
  27. this.noMore = true
  28. return
  29. }
  30. },
  31. methods: {
  32. async recordList() {
  33. const res = await uni.$http.get('/api/recordList', this.data)
  34. // console.log(res.data); // 每次请求是十条数据
  35. if (res.statusCode !== 200) return uni.$showErrorMsg("获取记录失败")
  36. this.list = [...this.list, ...res.data.records]
  37. this.total = res.data.total
  38. },
  39. onPullDownRefresh() {
  40. this.data.page = 1
  41. this.total = 0
  42. this.list = []
  43. this.record()
  44. setTimeout(() => {
  45. uni.stopPullDownRefresh()
  46. }, 1000);
  47. },
  48. onReachBottom() {
  49. if (this.data.page * this.data.limit >= this.total) {
  50. this.noMore = true
  51. return
  52. }
  53. this.data.page++ // 让页码值自增+1
  54. this.record()
  55. },
  56. }
  57. }
  58. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/221235
推荐阅读
相关标签
  

闽ICP备14008679号