exingdeyipi
exingdeyipi
  • 发布:2023-08-01 11:23
  • 更新:2023-09-27 18:22
  • 阅读:138

【报Bug】组件使用v-for渲染作用域插槽,在数据切换后渲染的内容与数据不一致

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: Windows 11 家庭中文版 22H2 22621.1992

HBuilderX类型: 正式

HBuilderX版本号: 3.8.7

第三方开发者工具版本号: Stable 1.06.2307250win32-x64

基础库版本号: 3.0.0

项目创建方式: HBuilderX

示例代码:
//组件代码  
<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>

操作步骤:

按示例代码即可复现

预期结果:

在初始化渲染完成后,点击改变数据,应该正确渲染出 changeData 所对应的数据内容

实际结果:

点击改变数据后, changeData 中的 id='id4' 的这项数据没有渲染出来,且渲染的顺序与数据不一致

bug描述:

描述

使用v-for加作用域插槽封装的列表组件,在调用时先初始化源数据listA,这个过程渲染没问题。
当源数据从数据listA切换到数据listB时,渲染出的内容与listB中的数据不一致。

其他

这个问题在 HBuilder X 3.8.11.20230719-alpha 中也能复现,使用cli创建的项目也可以复现。

2023-08-01 11:23 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

这个问题你应该去微信社区中问一问 或者换一种写法 微信小程序好像不支持循环插槽

  • exingdeyipi (作者)

    我试了下用v-for循环插槽是可以的

    我先换种写法吧

    2023-08-02 14:25

  • 爱豆豆

    回复 exingdeyipi: 嗯嗯

    2023-08-02 14:30

4***@qq.com

4***@qq.com

我现在也遇到了一个这样的问题,列表数据变化了,但是界面上的显示不对,一部分是正确的,一部分是错乱的。找了半天原因,代码改动也没影响这块

要回复问题请先登录注册