,可以通过以下步骤实现:
import { VChip } from 'vuetify/lib'
<template>
<div>
<v-chip v-for="item in items" :key="item.id" :selected="item.selected" @click="toggleSelection(item)">
{{ item.label }}
</v-chip>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, label: 'Option 1', selected: false },
{ id: 2, label: 'Option 2', selected: false },
{ id: 3, label: 'Option 3', selected: false },
// 添加更多选项...
]
}
},
methods: {
toggleSelection(item) {
item.selected = !item.selected
}
}
}
</script>
在上述代码中,我们使用v-for指令遍历items数组,为每个选项创建一个v-chip组件。每个选项都有一个唯一的id、一个显示的label和一个selected属性来表示是否被选中。点击v-chip组件时,通过toggleSelection方法切换选中状态。
computed: {
selectedItems() {
return this.items.filter(item => item.selected)
}
}
在上述代码中,我们使用Array的filter方法过滤出selected属性为true的选项,然后返回这些选项的数组。
<template>
<div>
<div v-for="item in selectedItems" :key="item.id">
{{ item.label }}
</div>
</div>
</template>
在上述代码中,我们使用v-for指令遍历selectedItems数组,将选中的值展示出来。
这样,你就可以从v-chip组中获取多个选定值了。根据具体的业务需求,你可以进一步处理这些选定值,例如发送到后端进行处理或者在前端进行其他操作。
关于v-chip组件的更多信息和使用方法,你可以参考腾讯云的Vuetify文档:Vuetify - v-chip。
领取专属 10元无门槛券
手把手带您无忧上云