dict-tag.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view class="dict-tag" :class="tagClass">{{ label }}</view>
  3. </template>
  4. <script>
  5. import dictMixin from '@/utils/mixins/dictMixin';
  6. export default {
  7. name: 'DictTag',
  8. mixins: [dictMixin],
  9. props: {
  10. // 字典类型
  11. dictType: {
  12. type: String,
  13. required: true
  14. },
  15. // 字典值
  16. value: {
  17. type: [String, Number],
  18. required: true
  19. },
  20. // 是否带颜色样式
  21. colored: {
  22. type: Boolean,
  23. default: true
  24. }
  25. },
  26. data() {
  27. return {
  28. dictTypeList: [] // 将通过created动态设置
  29. };
  30. },
  31. computed: {
  32. // 字典标签
  33. label() {
  34. return this.getDictLabel(this.dictType, this.value, '');
  35. },
  36. // 标签样式类
  37. tagClass() {
  38. if (!this.colored) return '';
  39. const className = this.getDictClass(this.dictType, this.value, '');
  40. return className ? `tag-${className}` : '';
  41. }
  42. },
  43. created() {
  44. // 检查父组件是否已加载此字典
  45. let parentWithDict = this.findParentWithDict(this.dictType);
  46. if (parentWithDict) {
  47. // 父组件已加载此字典,使用父组件的字典数据
  48. if (parentWithDict.dictData && parentWithDict.dictData[this.dictType]) {
  49. this.$set(this.dictData, this.dictType, parentWithDict.dictData[this.dictType]);
  50. }
  51. } else {
  52. // 父组件未加载此字典,自己加载
  53. this.dictTypeList = [this.dictType];
  54. this.loadDict();
  55. }
  56. },
  57. methods: {
  58. // 查找已加载指定字典类型的父组件
  59. findParentWithDict(dictType) {
  60. let parent = this.$parent;
  61. while (parent) {
  62. if (parent.dictData && parent.dictData[dictType]) {
  63. console.log(`[DictTag] Found parent with dictionary ${dictType} already loaded`);
  64. return parent;
  65. }
  66. parent = parent.$parent;
  67. }
  68. console.log(`[DictTag] No parent found with dictionary ${dictType}, loading it independently`);
  69. return null;
  70. }
  71. }
  72. };
  73. </script>
  74. <style scoped>
  75. .dict-tag {
  76. display: inline-block;
  77. padding: 0 10rpx;
  78. height: 40rpx;
  79. line-height: 40rpx;
  80. font-size: 26rpx;
  81. color: #666;
  82. border-radius: 20rpx;
  83. text-align: center;
  84. white-space: nowrap;
  85. }
  86. .tag-primary {
  87. color: #fff;
  88. background-color: #409EFF;
  89. }
  90. .tag-success {
  91. color: #fff;
  92. background-color: #67C23A;
  93. }
  94. .tag-info {
  95. color: #fff;
  96. background-color: #909399;
  97. }
  98. .tag-warning {
  99. color: #fff;
  100. background-color: #E6A23C;
  101. }
  102. .tag-danger {
  103. color: #fff;
  104. background-color: #F56C6C;
  105. }
  106. .tag-secondary {
  107. color: #fff;
  108. background-color: #8A69F9;
  109. }
  110. .tag-default {
  111. color: #333;
  112. background-color: #E9E9E9;
  113. }
  114. </style>