【小程序】使用uni-app搭建小程序环境---列表上拉加载更多
<template> <view> <view class="banner" @click="goDetail(banner)"> <image class="banner-img" :src="banner.cover"></image> <view class="banner-title">{{ banner.title }}</view> </view> <view class="uni-list"> <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value, key) in listData" :key="key" @click="goDetail(value)"> <view class="uni-media-list"> <image class="uni-media-list-logo" :src="value.cover"></image> <view class="uni-media-list-body"> <view class="uni-media-list-text-top">{{ value.title }}</view> <view class="uni-media-list-text-bottom"> <text>{{ value.author_name }}</text> <text>{{ value.published_at }}</text> </view> </view> </view> </view> </view> <uni-load-more :status="status" :content-text="contentText" /> </view> </template> <script> import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'; var dateUtils = require('../../../common/util.js').dateUtils; export default { components: { uniLoadMore }, data() { return { banner: {}, listData: [], last_id: '', reload: false, status: 'more', contentText: { contentdown: '上拉加载更多', contentrefresh: '加载中', contentnomore: '没有更多' } }; }, onLoad() { this.getBanner(); this.getList(); }, onPullDownRefresh() { this.reload = true; this.last_id = ''; this.getBanner(); this.getList(); }, onReachBottom() { this.status = 'more'; this.getList(); }, methods: { getBanner() { let data = { column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名 }; uni.request({ url: 'https://unidemo.dcloud.net.cn/api/banner/36kr', data: data, success: data => { uni.stopPullDownRefresh(); if (data.statusCode == 200) { this.banner = data.data; } }, fail: (data, code) => { console.log('fail' + JSON.stringify(data)); } }); }, getList() { var data = { column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名 }; if (this.last_id) { //说明已有数据,目前处于上拉加载 this.status = 'loading'; data.minId = this.last_id; data.time = new Date().getTime() + ''; data.pageSize = 10; } uni.request({ url: 'https://unidemo.dcloud.net.cn/api/news', data: data, success: data => { if (data.statusCode == 200) { let list = this.setTime(data.data); this.listData = this.reload ? list : this.listData.concat(list); this.last_id = list[list.length - 1].id; this.reload = false; } }, fail: (data, code) => { console.log('fail' + JSON.stringify(data)); } }); }, goDetail: function(e) { // if (!/前|刚刚/.test(e.published_at)) { // e.published_at = dateUtils.format(e.published_at); // } let detail = { author_name: e.author_name, cover: e.cover, id: e.id, post_id: e.post_id, published_at: e.published_at, title: e.title }; uni.navigateTo({ url: '../list2detail-detail/list2detail-detail?detailDate=' + encodeURIComponent(JSON.stringify(detail)) }); }, setTime: function(items) { var newItems = []; items.forEach(e => { newItems.push({ author_name: e.author_name, cover: e.cover, id: e.id, post_id: e.post_id, published_at: dateUtils.format(e.published_at), title: e.title }); }); return newItems; } } }; </script> <style> .banner { height: 360upx; overflow: hidden; position: relative; background-color: #ccc; } .banner-img { width: 100%; } .banner-title { max-height: 84upx; overflow: hidden; position: absolute; left: 30upx; bottom: 30upx; width: 90%; font-size: 32upx; font-weight: 400; line-height: 42upx; color: white; z-index: 11; } .uni-media-list-logo { width: 180upx; height: 140upx; } .uni-media-list-body { height: auto; justify-content: space-around; } .uni-media-list-text-top { height: 74upx; font-size: 28upx; overflow: hidden; } .uni-media-list-text-bottom { display: flex; flex-direction: row; justify-content: space-between; } </style>
作者:smile.轉角
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我QQ

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通