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


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

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

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

    image.png

    ...

  • 相关阅读:
    asyncawait和promise的区别
    Centos8安装docker并配置Kali Linux图形化界面
    java计算机毕业设计高校开放式实验室管理系统源码+mysql数据库+系统+lw文档+部署
    上下游系统对接的沟通与协作
    【Qt】三种方式实现抽奖小游戏
    2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”
    iTunes Connect在线创建 App
    使用springboot每日推送早安问候语到用户微信
    使用Python构建强大的网络爬虫
    建立自己公众号题库系统
  • 原文地址:https://blog.csdn.net/jun_tong/article/details/132832561