uniapp列表有数据,把数据传入点击事件打印出来是undefined, 因为key是undefined

: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”
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值