实现思路
- 在模板中使用
v-on:click
指令绑定点击事件,并通过括号传递需要的参数,如列表项的索引值或特定属性值。
- 在Vue组件的
methods
中定义事件处理函数,该函数接收传递过来的参数进行相应处理。
完整Vue组件代码
<template>
<div>
<ul>
<li v-for="(item, index) in list" :key="index" @click="handleClick(index, item.specificProperty)">
{{ item.text }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
list: [
{ text: '列表项1', specificProperty: '属性值1' },
{ text: '列表项2', specificProperty: '属性值2' },
{ text: '列表项3', specificProperty: '属性值3' }
]
};
},
methods: {
handleClick(index, property) {
console.log(`点击了第 ${index + 1} 项,特定属性值为: ${property}`);
}
}
};
</script>