uniapp下拉刷新上拉加载

﹏ヽ暗。殇╰゛Y 2021-09-03 04:51 803阅读 0赞

一、需求

  1. 留言板主页,显示所有的留言信息,带有分页功能;上拉加载数据,下拉刷新数据

二、代码

在这里插入图片描述
1、pages.json
在这里插入图片描述
2、messageBoard.vue
在这里插入图片描述
用了 uniapp 提供的组件: uni-load-more.vue

  1. <uni-load-more :status="loadingText" :contentText="contentText" ></uni-load-more>
  2. const loadingTextObj = {
  3. more: 'more',
  4. noMore: 'noMore',
  5. loading: 'loading'
  6. }
  7. const contentTextObj = {
  8. contentdown: '上拉显示更多',
  9. contentrefresh: '正在加载...',
  10. contentnomore: '没有更多数据了'
  11. }
  12. data() {
  13. return {
  14. // 分页功能
  15. loadingText: loadingTextObj.more,
  16. loadingType: 0, // 定义加载方式 0---contentdown 1---contentrefresh 2---contentnomore
  17. contentText: contentTextObj,
  18. }
  19. },
  20. //下拉刷新
  21. onPullDownRefresh() {
  22. console.log('下拉刷新')
  23. this.initPageInfo()
  24. },
  25. //触底加载更多
  26. onReachBottom(e) {
  27. const _this = this
  28. console.log('触底加载更多')
  29. if (timer != null) {
  30. clearTimeout(timer)
  31. }
  32. timer = setTimeout(function() {
  33. if (_this.loadingType === 2) return false
  34. else _this.doMoreData()
  35. }, 1000)
  36. },
  37. methods: {
  38. initPageInfo() {
  39. const _this = this
  40. page = 1
  41. this.messageInfo = []
  42. this.doLoadingStatus(0)
  43. uni.showNavigationBarLoading()
  44. // 接口联调
  45. this.doMessageBoard()
  46. return false
  47. setTimeout(() => { // 这里面是模拟的假数据
  48. page++ // 得到数据之后page+1
  49. _this.messageInfo = messageData // messageData为自己造的假数据
  50. uni.hideNavigationBarLoading()
  51. uni.stopPullDownRefresh() // 得到数据后停止下拉刷新
  52. }, 1000)
  53. },
  54. // 加载状态
  55. doLoadingStatus(type) {
  56. if (type === 0) {
  57. this.loadingType = 0
  58. this.loadingText = loadingTextObj.more
  59. }else if (type === 1) {
  60. this.loadingType = 1
  61. this.loadingText = loadingTextObj.loading
  62. } else if (type === 2) {
  63. this.loadingType = 2
  64. this.loadingText = loadingTextObj.noMore
  65. }
  66. },
  67. // 获取留言板列表
  68. doMessageBoard() {
  69. const _this = this
  70. let opts={
  71. url: '/***',
  72. method: 'POST'
  73. }
  74. let param={
  75. page: page,
  76. pageSize: pageSize,
  77. }
  78. this.HTTP.httpFromDataRequest(opts, param).then(res => {
  79. if(res.data.success){
  80. const { data } = res.data
  81. if (data.list === null) {
  82. _this.doLoadingStatus(2)
  83. uni.hideNavigationBarLoading() // 关闭加载动画
  84. return
  85. }
  86. page++ // 得到数据之后page+1
  87. if (_this.messageInfo.length === 0) _this.messageInfo = data.list
  88. else _this.messageInfo = [..._this.messageInfo, ...data.list]
  89. if (data.list.length < pageSize) {
  90. _this.doLoadingStatus(2)
  91. } else{ // 将loadingType归0重置
  92. _this.doLoadingStatus(0)
  93. }
  94. uni.hideNavigationBarLoading(); // 关闭加载动画
  95. uni.stopPullDownRefresh() // 得到数据后停止下拉刷新
  96. }else{
  97. uni.showToast({
  98. title: res.data.message
  99. })
  100. }
  101. },error => { console.log(error);})
  102. },
  103. // 分页功能---加载更多数据
  104. doMoreData() {
  105. const _this = this
  106. if (this.loadingType !== 0) { // loadingType != 0 直接返回
  107. return false
  108. }
  109. this.doLoadingStatus(1)
  110. uni.showNavigationBarLoading()
  111. this.doMessageBoard()
  112. return false
  113. setTimeout(() => {
  114. const res = {
  115. data: null
  116. }
  117. res.data = '111'
  118. if (res.data === null) {
  119. _this.loadingType = 2
  120. _this.loadingText = loadingTextObj.noMore
  121. uni.hideNavigationBarLoading() // 关闭加载动画
  122. return
  123. }
  124. page++ // 每触底一次 page +1
  125. _this.messageInfo = [..._this.messageInfo, ...messageData]
  126. _this.loadingType = 0; // 将loadingType归0重置
  127. _this.loadingText = loadingTextObj.more
  128. uni.hideNavigationBarLoading(); // 关闭加载动画
  129. }, 3000)
  130. }, // end doMoreData
  131. }

发表评论

表情:
评论列表 (有 0 条评论,803人围观)

还没有评论,来说两句吧...

相关阅读