5

我正在使用 vue 选择。在下拉列表中,有标签(不仅是文本)。是否也可以有所选值的标签?

                            <div class="form-group row">
                                <label for="project_status_id"  class="col-sm-3 col-form-label">Projekt Status</label>
                                <div class="col-sm-9">
                                    <v-select  :options="resources.activeProjectStatus" :reduce="project_status_id => project_status_id.id"  v-model="form.project_status_id" label="name" id="project_status_id" placeholder="Projekt Status" :class="$vSelectStyle($v.form.project_status_id)">
                                        <template v-slot:option="option" >
                                            <div v-html="option.status_label" class="mb-1">
                                            </div>
                                        </template>
                                    </v-select>
                                    <template v-if="$v.form.project_status_id.$error">
                                        <p class="text-danger" v-if="!$v.form.project_status_id.required">
                                            Projekt - Status ist erforderlich!
                                        </p>
                                    </template>
                                </div>
                            </div>

在此处输入图像描述

4

1 回答 1

1

假设您想要 的 HTML status_label,还假设它status_label是模板字符串或类似字符串,则使用selected-option插槽内容与您的选项插槽相同的插槽,而无需class附加。

如前所述,下面示例中的关键部分是selected-option插槽:

<!-- Using OP's `option` key -->
<template v-slot:selected-option="option">
  <div v-html="option.status_label"></div>
</template>

下面的示例是Vue-Select 的 Codepen 示例的一个分支,对答案进行了修改。

Vue.config.productionTip = false;
Vue.component('v-select', VueSelect.VueSelect);
new Vue({
  el: '#app',
  data: {
    options: [
      {
        name: `<span style="padding: 4px; background: green; border-radius: 0.25rem; color: white;">Foo</span>`
      },
      {
        name: `<span style="padding: 4px; background: orange; border-radius: 0.25rem; color: white;">Bar</span>`
      },
      {
        name: `<span style="padding: 4px; background: red; border-radius: 0.25rem; color: white;">Baz</span>`
      }
    ]
  }
});
body {
  font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
}

h1 {
  font-size: 26px;
  font-weight: 600;
  color: #2c3e5099;
  text-rendering: optimizelegibility;
  -moz-osx-font-smoothing: grayscale;
  -moz-text-size-adjust: none;
}

#app {
  max-width: 30em;
  margin: 1em auto;
}
<script src="https://unpkg.com/vue@latest"></script>
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:600">

<div id="app">
  <h1>Vue Select</h1>
  <v-select :options="options" label="label">
    <template v-slot:option="option" >
      <div v-html="option.name" style="padding: 2px 0;"></div>
    </template>
    <template v-slot:selected-option="option">
      <div v-html="option.name"></div>
    </template>
  </v-select>
</div>

于 2020-03-05T17:13:43.770 回答