uni-app 之 下拉刷新,上拉加载,获取网络列表数据

灰太狼 2023-10-15 21:22 45阅读 0赞

#

uni-app 之 下拉刷新,上拉加载,获取网络列表数据

format_png

image.png

  1. <template>
  2. <view>
  3. <!-- 车源模块 -->
  4. --- uni.request 网络请求API接口 ---
  5. <view v-for="(item) in newsArr" :key="item.id" style="display: flex; margin-top: 12rpx;">
  6. <!-- 免费的测试接口 -->
  7. <image :src="item.picurl" mode="aspectFill" style="width: 300rpx;height: 200rpx;margin-left: 12rpx;">
  8. </image>
  9. <view style="display: flex;flex-direction: column; flex-wrap: wrap;width: 400rpx; margin-left: 12rpx;">
  10. <text style=" font-weight:bold;"> {
  11. {item.title}}</text>
  12. <text style="font-size:12rpx;">{
  13. {item.posttime}}</text>
  14. <text style="color:#d81e06">{
  15. {item.hits}}</text>
  16. </view>
  17. </view>
  18. <view v-if="!newsArr.length">
  19. 没有数据的时候展示图片
  20. <image src="../../static/logo.png"></image>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. newsArr: [],
  29. currentPage: 1,
  30. }
  31. },
  32. onLoad() {
  33. this.getNewsData()
  34. },
  35. onPullDownRefresh() {
  36. console.log("111 111 下拉 " + this.currentPage)
  37. this.newsArr = [] // 下拉刷新时,清空集合
  38. this.currentPage = 1 // 下拉刷新时,page恢复初始1
  39. this.getNewsData() // 下拉刷新时,重新获取数据
  40. },
  41. onReachBottom() {
  42. console.log("111 111 到底啦 " + this.currentPage)
  43. uni.stopPullDownRefresh()
  44. this.currentPage++; // 上拉加载时,page+1
  45. this.getNewsData(50); // 传输的cid是在有头部tabbar点击切换的时候才用到,正常的列表可以删除
  46. },
  47. methods: {
  48. getNewsData(id = 50) {
  49. uni.request({
  50. url: "https://ku.qingnian8.com/dataApi/news/newslist.php",
  51. data: {
  52. // num:8,//展示几条,默认8条
  53. // cid:50,//列表分类:50国内,51国外,52体育,53软件,
  54. cid: id, // 传输的cid是在有头部tabbar点击切换的时候才用到,正常的列表可以删除
  55. page: this.currentPage
  56. },
  57. // timeout:"6000",
  58. success: res => {
  59. console.log(res) // log打印获取的数据
  60. // this.newsArr = res.data
  61. this.newsArr = [...this.newsArr, ...res.data]
  62. uni.stopPullDownRefresh() // 加载出数据后关闭下拉动画
  63. },
  64. })
  65. }
  66. }
  67. }
  68. </script>
  69. <style>
  70. </style>
  71. // d81e06 紅
  72. // f4ea2a 黃
  73. // 1afa29 綠
  74. // 1296db 藍
  75. // 13227a 青
  76. // d4237a 紫
  77. // ffffff 白
  78. // 2c2c2c 黑

注意:pages.json里的enablePullDownRefresh要改为true

format_png 1

image.png

发表评论

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

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

相关阅读