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;
		}
	}
}
相关推荐
iOS阿玮8 小时前
鸿蒙激励的羊毛,你"薅"到了么?
uni-app·app·apple
ResponseState20011 小时前
安卓原生写uniapp插件手把手教学调试、打包、发布。
前端·uni-app
h_654321013 小时前
uniapp app端解析图片的经纬度、方位角
uni-app
h_654321013 小时前
uniapp app端获取指定路径下的所有图片
uni-app
雪芽蓝域zzs13 小时前
uniapp真机运行鸿蒙定位报getLocation:fail maybe not obtain GPS Permission
华为·uni-app·harmonyos
初遇你时动了情13 小时前
不用每个请求都写获取请求 类似无感刷新逻辑 uniapp vue3 react实现方案
javascript·react.js·uni-app
apollo_qwe1 天前
解决移动端键盘遮挡痛点
uni-app
脾气有点小暴2 天前
scroll-view分页加载
前端·javascript·uni-app
脾气有点小暴2 天前
uniapp自定义头部导航
前端·uni-app
前端 贾公子2 天前
[uniapp][swtich开关]阻止切换状态(类似阻止事件冒泡)
uni-app