根据字典值回显,有颜色的

背景

本项目以若依前端vue2版本为例,项目中有根据字典值回显文本的函数selectDictLabel,但是有时候我们需要带颜色的回显,大概这样的在这里插入图片描述

用法

<template v-slot='scope'>
  <dict-label :options="dangerLevelOptions" :value="scope.row.dangerLevel" />
</template>
<script>
  import DictLabel from '@/components/DictTag/label';
  export default{
    components: {
      DictLabel
    },
    data() {
      return {
        dangerLevelOptions: [
          { value: '1', label: '一般隐患', color: '#1786D9' },
          { value: '2', label: '较大隐患', color: '#FCD46E' },
          { value: '3', label: '重大隐患', color: '#FF7826' },
          { value: '4', label: '特别重大隐患', color: '#FF756F' },
        ]
      }
    }
  }
</script>

源码

以下仅供参考,还可以将颜色优化为type:default | success | warning | danger

<template>
  <div>
    <template v-for="(item, index) in options">
      <template v-if="values.includes(item.value)">
        <span
          :key="item.value"
          :index="index"
          :style="{ color: item.color }"
        >{{ item.label }}</span>
      </template>
    </template>
  </div>
</template>

<script>
export default {
  name: "DictLabel",
  props: {
    options: {
      type: Array,
      default: null,
    },
    value: [Number, String, Array],
  },
  computed: {
    values() {
      if (this.value !== null && typeof this.value !== 'undefined') {
        return Array.isArray(this.value) ? this.value : [String(this.value)];
      } else {
        return [];
      }
    },
  },
};
</script>
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值