uni-app中for渲染时,不可以把item作为事件参数嘛?

uni-app中for渲染时,不可以把item作为事件参数嘛?

<template v-for="(item,index) in data"> <uni-card :title="(index+1)+''" :key="index" thumbnail="![Packing](/static/Menu/Packing.png)" :extra="item.Count" note="true">
    <template v-slot:footer>  
        <view style="flex-direction: row; justify-content: space-around">  
            <button type="warn" size="mini" style="width: 200rpx" @click="delete(index)">删除</button>  
            <button type="default" size="mini" style="width: 200rpx" @click="modify(index)">修改</button>  
        </view>  
    </template>  

</uni-card>  
</template>

2021-02-15 20:20


更多关于uni-app中for渲染时,不可以把item作为事件参数嘛?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

解决了,应该是delete命名冲突

更多关于uni-app中for渲染时,不可以把item作为事件参数嘛?的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中,确实可以将v-for循环的item作为事件参数传递。你的代码问题在于直接使用了index作为事件参数,而没有传递item对象。

正确的写法应该是:

  1. 在模板中传递item参数:
<button @click="delete(item)">删除</button>
<button @click="modify(item)">修改</button>
  1. 在methods中接收:
methods: {
  delete(item) {
    console.log('要删除的项:', item);
    // 删除逻辑
  },
  modify(item) {
    console.log('要修改的项:', item);
    // 修改逻辑
  }
}
回到顶部