Vuetify - 如何自定义 <v-select> 中选项的样式?

pas*_*ner 4 javascript css vue.js material-design vuetify.js

我正在尝试找到一种自定义<v-select>选项样式的方法。backgroundColor我想根据选项的值提供不同的值。提供的所有内容都:items将被修复(始终相同),因此我需要找到一种方法来通过其特定文本值或声明唯一的类/id 来检测选项。对此最好的方法是什么?谢谢。

当前代码:

<v-select
   :items="[
      'This is yellow option', // Yellow background.
      'This is blue option', // Blue background.
      'This is grey option' // Grey background.
   ]"
>
</v-select>
Run Code Online (Sandbox Code Playgroud)

是我想要的结果示例的链接。

<v-select
   :items="[
      'This is yellow option', // Yellow background.
      'This is blue option', // Blue background.
      'This is grey option' // Grey background.
   ]"
>
</v-select>
Run Code Online (Sandbox Code Playgroud)
.yellow {
  background-color: yellow;
}

.blue {
  background-color: blue;
}

.grey {
  background-color: grey;
}
Run Code Online (Sandbox Code Playgroud)

Bou*_*him 6

您可以按如下方式使用插槽item

  <v-select :items="items" label="Standard">
            <template #item="{item}">
              <span :class="[{'yellow':item.includes('yellow')},
                    {'blue':item.includes('blue')},{'grey':item.includes('grey')}]"> 
                      {{item}}</span>
           </template>

  </v-select>
Run Code Online (Sandbox Code Playgroud)
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    items:[ 'This is yellow option', // Yellow background.
      'This is blue option', // Blue background.
      'This is grey option' // Grey background.,
    ]
  }),
})
Run Code Online (Sandbox Code Playgroud)

现场演示