:key很关键,如果 :key 是undeifined, 那么 toggle 事件传入的 item 打印出来将会是 undefined

<view class="cu-list-content" v-for="(item, index) in products" :key="item.id">
    {{JSON.stringify(item)}}
    <view class="cu-item" @tap="toggle(item)">
        <view class="content">
            <text class="text-cut">{{item.productName}} </text>
        </view>
    </view>
    <view class="productInfo" :class="item.show?'show':'hide'">
        <view class="productItem">{{item.productTypeName }}  </view>
        <view class="productItem">宽度:{{item.width }} 毫米</view>
        <view class="productItem">长度:{{item.height }} 毫米</view>
    </view>
</view>
data() {
    return {
        products : [{
            
            show:false,
            url :'',
            productName : '[平开窗推拉平柜重型系列60]',
            productTypeName: '--',
            num:1,
            productTypeName : '[平开窗推拉平柜重型系列60]',
            width :  '1000 毫米',
            height :  '1000 毫米',
            outerColorName:'宫廷白',
            innerColorName:'宫廷白',
            handleMode:'斯高执手',
            glassType :'磨砂玻璃',
            remark:"--"
        }, {
            
            show:false,
            url :'',
            productName : '[平开窗推拉平柜重型系列60]',
            productTypeName: '--',
            num:1,
            productTypeName : '[平开窗推拉平柜重型系列60]',
            width :  '1000 毫米',
            height :  '1000 毫米',
            outerColorName:'宫廷白',
            innerColorName:'宫廷白',
            handleMode:'斯高执手',
            glassType :'磨砂玻璃',
            remark:"--"
        }, {
            
            show:false,
            url :'',
            productName : '[平开窗推拉平柜重型系列60]',
            productTypeName: '--',
            num:1,
            productTypeName : '[平开窗推拉平柜重型系列60]',
            width :  '1000 毫米',
            height :  '1000 毫米',
            outerColorName:'宫廷白',
            innerColorName:'宫廷白',
            handleMode:'斯高执手',
            glassType :'磨砂玻璃',
            remark:"--"
        }]
    }
},
methods: {
    toggle(item) {
        console.log('item :>> ',item)
    }
}

在这里插入图片描述

原因:

在这里插入图片描述

解决方法:

  1. 自己 遍历 products ,给每一个 item 添加值不重复的 id 字段;
  2. v-for=“(item, index) in products” :key=“index”
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐