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;
		}
	}
}
相关推荐
Rysxt_21 小时前
uni-app 使用教程:从入门到发布
uni-app
homelook2 天前
uniapp蓝牙demo
uni-app
2501_915909063 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
郑州光合科技余经理3 天前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
wangdaoyin20103 天前
UniApp中使用LivePlayer进行视频或在流媒体播放
uni-app·liveplayer·h5播放视频
2501_915106323 天前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者83 天前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_916008893 天前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
一室易安3 天前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app
yilan_n3 天前
鸿蒙应用上传
vue.js·华为·uni-app