//组件代码
<template>
<view>
<template :key="item[props.listKey] || index" v-for="(item, index) in props.dataList">
<slot :record="item" :index="index"></slot>
</template>
</view>
</template>
<script setup>
const props = defineProps({
listKey: String,
dataList: Array
})
</script>
<style></style>
//复现问题的代码
<template>
<view class="content">
<TestList :dataList="list" listKey="id" v-slot="{ record }">
<view class="itemA" :style="{color:record.color}">
<span>{{record.label}}</span>
<span>{{record.text}}</span>
<view >{{record.color}}</view>
</view>
</TestList>
<view class="btn">
<button @click="changeList('init')">还原数据</button>
<button @click="changeList('change')">改变数据</button>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
const initData = [
{ id: 'id1', label: '111', color: 'black', text: '第一项(黑色)' },
{ id: 'id2', label: '222', color: 'black', text: '第二项(黑色)' },
{ id: 'id3', label: '333', color: 'pink', text: '第三项(粉色)' },
{ id: 'id4', label: '444', color: 'gray', text: '第四项(灰色s)' },
]
const changeData = [
{ id: 'id2', label: '222', color: 'black', text: '第二项(黑色)' },
{ id: 'id1', label: '111', color: 'blue', text: '第一项(蓝色)' },
{ id: 'id4', label: '444', color: 'green', text: '第四项(绿色s)' },
{ id: 'id5', label: '555', color: 'red', text: '第五项(红色)' },
]
const list = ref(initData)
const changeList = (type) => {
if (type === 'init') {
list.value = initData
} else {
list.value = changeData
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.itemA{
padding: 10rpx;
border: 1rpx solid #cccccc;
margin: 10rpx;
}
.btn {
width: 100%;
display: flex;
justify-content: space-between;
margin-top: 40rpx;
}
</style>
exingdeyipi (作者)
我试了下用v-for循环插槽是可以的
我先换种写法吧
2023-08-02 14:25
爱豆豆
回复 exingdeyipi: 嗯嗯
2023-08-02 14:30