【问题标题】:Vue Element UI - html inside <el-select>Vue Element UI - <el-select> 内的 html
【发布时间】:2021-04-23 09:34:26
【问题描述】:

我想为每个条目实现一个带有不同颜色标签的选择元素:

我的代码如下所示:

var Main = {
  data() {
    return {
      selectedState: null,
      processStates: [
        {
          value: 0,
          label: 'New',
          color: 'ffffff'
        },
        {
          value: 1,
          label: 'Ready',
          color: 'ff9933'
        },
        {
          value: 2,
          label: 'Running',
          color: '008000'
        },
        {
          value: 3,
          label: 'Rejected',
          color: 'cc0000'
        },
        {
          value: 4,
          label: 'Terminated',
          color: '2E9AFE'
        }
      ]
    }
  },
  
   methods: {}
}

var Ctor = Vue.extend(Main);
new Ctor().$mount('#app');
@import url("//unpkg.com/element-ui@2.4.4/lib/theme-chalk/index.css");
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.4.11/lib/index.js"></script>

<div id="app">
    <el-select v-model="selectedState" style="width:200px">
      <el-option
        v-for="state in processStates"
        :key="state.value"
        :label="state.label"
        :value="state.value"
      >
        <span>
          <el-tag :style="'background-color:#' + state.color">&nbsp;</el-tag> {{ state.label }}
        </span>
      </el-option>
    </el-select>
</div>

如您所见,我设法将 html 注入到选项标签中并获得了所需的结果。
However, I would like to have the same html when one option is selected.

想要的结果:

知道如何实现吗?

【问题讨论】:

    标签: vuejs2 element-ui


    【解决方案1】:

    您必须为此使用prefix 插槽。如下所示,我也将 selectedState 更改为对象,但您仍然可以使用 string 值,但是您必须进行查找以获取颜色

    var Main = {
      data() {
        return {
          selectedState: { color: 'ffffff'},
          processStates: [
            {
              value: 0,
              label: 'New',
              color: 'ffffff'
            },
            {
              value: 1,
              label: 'Ready',
              color: 'ff9933'
            },
            {
              value: 2,
              label: 'Running',
              color: '008000'
            },
            {
              value: 3,
              label: 'Rejected',
              color: 'cc0000'
            },
            {
              value: 4,
              label: 'Terminated',
              color: '2E9AFE'
            }
          ]
        }
      },
      
       methods: {}
    }
    
    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#app');
    @import url("//unpkg.com/element-ui@2.4.4/lib/theme-chalk/index.css");
    
    .el-input--prefix .el-input__inner { 
      padding-left: 40px;
    }
    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/element-ui@2.4.11/lib/index.js"></script>
    
    <div id="app">
    <el-select v-model="selectedState" value-key="value" style="width:200px">
      <template slot="prefix">
        <el-tag class="prefix" :style="`background-color: #${selectedState.color}`"/>
      </template>
      <el-option
        v-for="state in processStates"
        :key="state.value"
        :label="state.label"
        :value="state"
      >
        <span>
          <el-tag :style="'background-color:#' + state.color">&nbsp;</el-tag> {{ state.label }}
        </span>
      </el-option>
    </el-select>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多