dictLabel.vue 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <template>
  2. <el-Tag :type="state.tagType">{{ state.label }}</el-Tag>
  3. </template>
  4. <script lang="ts" setup>
  5. import { useUserInfo } from '/@/stores/userInfo';
  6. import { reactive, onMounted, watch } from 'vue';
  7. const props = defineProps({
  8. code: String,
  9. value: null,
  10. propLabel: {
  11. type: String,
  12. default: 'value',
  13. },
  14. propValue: {
  15. type: String,
  16. default: 'code',
  17. },
  18. defaultValue: {
  19. type: String,
  20. default: '-',
  21. },
  22. });
  23. const state = reactive({
  24. label: props.defaultValue as string,
  25. tagType: "primary" as "success" | "warning" | "info" | "primary" | "danger"
  26. });
  27. onMounted(() => {
  28. setDictValue(props.value);
  29. });
  30. watch(
  31. () => props.value,
  32. (newValue) => setDictValue(newValue)
  33. );
  34. const setDictValue = (value: any) => {
  35. const dict = useUserInfo().dictList[props.code]?.find((x: any) => x[props.propValue] == value);
  36. if (dict) {
  37. state.label = dict[props.propLabel] || props.defaultValue;
  38. state.tagType = dict.tagType ?? "primary";
  39. }
  40. }
  41. </script>