You need to enable JavaScript to run this app. 如何在Vuetify select下拉菜单中使用图标作为选项标签
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vuetify select下拉菜单中使用图标作为选项标签

这个需求可以完美实现,你当前的代码已经完成了下拉候选选项的图标渲染,只需要额外补充选中态的自定义插槽即可。

你目前只使用了v-slot:item插槽,该插槽仅作用于下拉展开后的候选选项列表,选中后输入框内的展示内容默认只会读取item-text绑定的纯文本字段,因此需要新增v-slot:selection插槽自定义选中后的展示样式。

修改后的完整代码

HTML部分

<div id="app">
  <v-app class="container">
    <v-select
      v-model="select"
      :items="permissions"
      label="Select"
      item-text="name"
    >
      <!-- 下拉选项自定义插槽 -->
      <template v-slot:item="slotProps" >
        <i :class="['mr-2', 'mdi', slotProps.item.flag]"></i>
        {{slotProps.item.name}}
      </template>
      <!-- 新增:选中后输入框内展示的自定义插槽 -->
      <template v-slot:selection="slotProps">
        <i :class="['mr-2', 'mdi', slotProps.item.flag]"></i>
        {{ slotProps.item.name }}
      </template>
     </v-select>
  </v-app>
</div>

JS部分

你的原有JS逻辑无需调整,可直接复用:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: {
    select: null,
    permissions: [
      {
        name: "Global",
        flag: "mdi-earth"
      },
      {
        name: "Private",
        flag: "mdi-lock"
      }
   ],
  }
})

如果需要仅显示图标、隐藏文字,直接删除两个插槽内的{{ slotProps.item.name }}字段即可。注意提前确认项目已正确引入Material Design Icon字体库,避免图标无法正常渲染。

内容的提问来源于stack exchange,提问作者M. Straw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 04:15:03