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;
		}
	}
}
相关推荐
河北清兮网络科技1 天前
短剧 APP 产品说明
小程序·uni-app·短剧
宠友信息1 天前
一套基于uniapp+springboot完整社区系统是如何实现的?友猫社区源码级功能解析
java·spring boot·后端·微服务·微信·uni-app
碎像2 天前
掌握uniapp发布微信小程序、App(Android)
微信小程序·小程序·uni-app
stpzhf2 天前
uniapp nvue组件多个text在一行并且高亮其中一些文字
前端·javascript·uni-app
qq_316837752 天前
制作uniapp原生插件 在本地离线打包中测试 集成在云打包中
uni-app
Fate_I_C3 天前
uniappx 鸿蒙运行包制作失败
华为·uni-app·uniapp·harmonyos
chQHk57BN3 天前
跨平台前端开发:用Flutter和UniApp一次编写多端运行
flutter·uni-app
自然 醒3 天前
uni-app开发微信小程序,如何使用towxml去渲染md格式和html标签格式的内容?
微信小程序·uni-app·html
CHB3 天前
uni-agent,你的数字员工来了
人工智能·uni-app·vibecoding
h_jQuery3 天前
uniapp使用canvas实现逐字书写任意文字内容,后合成一张图片提交
前端·javascript·uni-app