index.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <el-select class="w-1/1" v-bind="attrs">
  3. <el-option
  4. v-for="(item, index) in options"
  5. :key="index"
  6. :label="item.label"
  7. :value="item.value"
  8. />
  9. </el-select>
  10. </template>
  11. <script lang="ts" setup>
  12. import request from '@/config/axios'
  13. import { isEmpty } from '@/utils/is'
  14. defineOptions({ name: 'CurrencySelect' })
  15. // 接受父组件参数
  16. interface Props {
  17. labelField?: string // 字典类型
  18. valueField?: string // 字典值类型
  19. restful?: string // api 接口
  20. }
  21. const props = withDefaults(defineProps<Props>(), {
  22. labelField: 'nickname',
  23. valueField: 'id',
  24. restful: '/system/user/simple-list'
  25. })
  26. const attrs = useAttrs()
  27. const options = ref<any[]>([]) // 下拉数据
  28. const getOptions = async () => {
  29. options.value = []
  30. if (isEmpty(props.restful)) {
  31. return
  32. }
  33. // TODO 只支持 GET 查询,复杂下拉构建条件请使用业务表单
  34. const data = await request.get({ url: props.restful })
  35. if (Array.isArray(data)) {
  36. options.value = data.map((item: any) => ({
  37. label: item[props.labelField],
  38. value: item[props.valueField]
  39. }))
  40. return
  41. }
  42. console.log(`接口[${props.restful}] 返回结果不是一个数组`)
  43. }
  44. onMounted(() => {
  45. getOptions()
  46. })
  47. </script>