uniapp+vue3 随机、换一批

案例

代码

html 复制代码
<view class="list">
	<view class="row" v-for="(item,index) in showList" :key="index">
		<view class="txt">{{ index + 1 }}、{{item.title}}</view>
		<view class="arrow">
			<fui-icon name="arrowright" color="#6C6B6B" size="34"></fui-icon>
		</view>
	</view>
	<view class="replaceBtn" @click="replaceList">
		<image src="/pages/userSub/static/images/replace.png" mode="aspectFill"></image>换一批
	</view>
</view>
javascript 复制代码
<script setup>
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import i from '@/libs/common/index.js'
	import api from '@/request/api.js'
	import {
		nextTick,
		ref,
		shallowRef,
		reactive
	} from "vue";
	import {
		userStore
	} from '@/store/userStore.js'
	import {
		commonStore
	} from '@/store/commonStore.js'
	const user = userStore()
	const common = commonStore()
	
	const list = ref([])
	const showList = ref([]) // 新增显示用的列表
	const page = ref(1)
	const size = ref(10000)
	const loadState = ref(1)
	onShow(() => {
		getKeFuList()
		getKeFuInfo()
	})

	function getKeFuList() {
		api.getKeFuList({
			page: page.value,
			size: size.value,
		}).then(res => {
			if (res.code == 1) {
				list.value = res.data
				// 初始化显示三条
				replaceList()
			}
		})
	}

	function replaceList() {
		// 随机打乱数组并取前三个
		const shuffled = [...list.value].sort(() => Math.random() - 0.5)
		showList.value = shuffled.slice(0, 3)
	}

</script>
css 复制代码
.list {
	margin: 0 30rpx 16rpx;
	background: #FFFFFF;
	border-radius: 20rpx 20rpx 20rpx 20rpx;
	padding: 0 30rpx;

	.row {
		padding: 30rpx 0;
		border-bottom: 1rpx solid #E4E4E4;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		color: #3D3D3D;

		.txt {
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			width: 100%;
		}

		.arrow {
			flex-shrink: 0;
			margin-left: 15rpx;
		}
	}

	.replaceBtn {
		font-size: 28rpx;
		color: #606266;
		padding: 30rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 24rpx;
			height: 24rpx;
			margin-right: 10rpx;
		}
	}
}
相关推荐
PedroQue9910 小时前
unix-utils v0.2.0 发布:modal 全端统一,自绘窗口解决输入框难题
前端·uni-app
2301_32241428043 天前
springboot学生课堂出勤统计系统15835-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·mysql·随机森林·微信小程序·uni-app·微信开发者工具·推荐算法
2501_915921435 天前
iOS应用从开发到上架App Store的完整步骤详解
android·ios·小程序·https·uni-app·iphone·webview
小蒜学长6 天前
基于SpringBoot+uni-app的闪洁家政系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·uni-app·家政服务
anyup_前端梦工厂6 天前
uni-app 模板 Starter 一键清理 Skill 发布
前端·人工智能·uni-app
anyup_前端梦工厂6 天前
将uni-app工程迁移uni-app x,AI 能帮我们做什么?
前端·人工智能·uni-app
蒿豪郝灬号7 天前
UniApp接入鸿蒙版阿里百川
华为·uni-app·harmonyos
2501_915921438 天前
详细教程:使用Perfdog工具测量iOS应用的功耗性能与优化方法
android·macos·ios·小程序·uni-app·cocoa·iphone
梦曦i8 天前
uni-app、uni-app x 与 Flutter 深度对比:2026年跨平台开发框架选型指南
flutter·uni-app·uni-app x
普马萨特8 天前
uni-app x 定位踩坑实录:H5/小程序/App 统一经纬度与逆地理编码
uni-app