LangSelector.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <script setup lang="ts">
  2. import { fetchI18nList } from '@/api/modules/anycallService'
  3. // 定义组件属性
  4. interface Props {
  5. modelValue?: string
  6. disabled?: boolean
  7. placeholder?: string
  8. style?: string | object
  9. className?: string
  10. }
  11. // 定义组件事件
  12. interface Emits {
  13. (e: 'update:modelValue', value: string | undefined): void
  14. }
  15. // 设置默认属性
  16. const props = withDefaults(defineProps<Props>(), {
  17. disabled: false,
  18. placeholder: '请选择',
  19. style: 'width: 140px;',
  20. })
  21. // 定义事件触发器
  22. const emit = defineEmits<Emits>()
  23. // 创建本地响应式变量用于v-model绑定
  24. const localValue = ref(props.modelValue)
  25. // 监听props中的modelValue变化,同步到本地变量
  26. watch(
  27. () => props.modelValue,
  28. (newValue) => {
  29. localValue.value = newValue
  30. },
  31. { immediate: true }
  32. )
  33. const options = ref<Array<{value: string, name: string}>>([])
  34. const fetchData = async () => {
  35. try {
  36. const res = await fetchI18nList()
  37. if (res.code === 0) {
  38. console.log(res.data,3333)
  39. options.value = res.data
  40. // // 将API返回的数据转换为select需要的格式
  41. // options.value = res.data.content.map((item: any) => ({
  42. // value: item.id,
  43. // name: item.name
  44. // }))
  45. } else {
  46. console.error('获取 I18n 失败:', res.msg)
  47. }
  48. } catch (error) {
  49. console.error('获取 I18n 失败:', error)
  50. }
  51. }
  52. onMounted(() => {
  53. fetchData()
  54. })
  55. </script>
  56. <template>
  57. <el-select
  58. v-model="localValue"
  59. :placeholder="placeholder"
  60. :style="style"
  61. :disabled="disabled"
  62. :class="className"
  63. clearable
  64. filterable
  65. @update:model-value="(value) => emit('update:modelValue', value)"
  66. >
  67. <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value" />
  68. </el-select>
  69. </template>