Den*_*Den 6 vue.js vuetify.js vuejs3 vuetifyjs3
我在 Vue 3 应用程序中使用 Vuetify 3 中的 VSelect,并且尝试使用项目槽。但我的 VSelect 选项变得不可选取
我有这个 VSelect 元素:
<v-select
v-model="tag"
:items="tags"
variant="solo"
label="Tags" >
<template #item="{ item }" >
<v-list >
<v-list-item :title="item.title" />
</v-list>
</template>
</v-select>
Run Code Online (Sandbox Code Playgroud)
数据中的标签和标签:
tag: null,
tags: [
{ title: 'example1', value: 0 },
{ title: 'example2', value: 1 },
],
Run Code Online (Sandbox Code Playgroud)
在输出中我有这个带有选项的选择,但选项不可选择:

那么如何在 Vuetify 3 中为 VSelect 组件设置一个带有可选选项的插槽“item”?
props传递给槽的对象包含一个回调onClick,您需要绑定它才能使选择工作:
<template #item="{ item, props: { onClick } }" >
<v-list-item :title="item.title" @click="onClick"/>
</template>
Run Code Online (Sandbox Code Playgroud)
目前文档有点稀疏,给出的类型为Record<string, unknown>. 在 Vuetify 3.4 中,其他值是key、和,它们是来自底层VVritualScroll 的模板引用,用于更新滚动条的高度title。valueref
当使用带有 prop 的组件title(如VListItem)时,您可以绑定整个props对象:
<template #item="{ props }" >
<v-list-item v-bind="props"/>
</template>
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,#item插槽已经将其内容渲染到 a 中v-list,无需再次包装)
这是一个片段:
const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
createApp({
setup(){
return {
tag: ref(null),
tags: [
{ title: 'example1', value: 0 },
{ title: 'example2', value: 1 },
],
}
}
}).use(vuetify).mount('#app')Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3.1.6/dist/vuetify.min.css" />
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app" class="d-flex justify-center">
<v-app>
<div>
<v-select
style="width: 300px;"
v-model="tag"
:items="tags"
variant="solo"
label="Tags"
>
<template #item="{ item, props: {onClick, title, value} }" >
<v-list-item :title="item.title" @click="onClick"/>
</template>
</v-select>
Selected: {{tag}}
</div>
</v-app>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3.1.6/dist/vuetify.min.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2257 次 |
| 最近记录: |