index.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!-- 动态组件 -->
  2. <template>
  3. <app-tabs class="app-tabs--primary" :data-list="dataList" v-model:data-index="dataIndex" prop-label="title"
  4. @change="onTabChange">
  5. <template #navbar>
  6. <slot></slot>
  7. </template>
  8. <component :is="componentMap.get(componentId)" :code="componentId" v-bind="options" v-if="componentId" />
  9. </app-tabs>
  10. </template>
  11. <script lang="ts" setup>
  12. import { shallowRef, PropType, onMounted, useAttrs, computed } from 'vue'
  13. import { useAuth } from '@/hooks/auth'
  14. import AppTabs from '@/components/base/tabs/index.vue'
  15. const props = defineProps({
  16. code: String,
  17. options: Object,
  18. // 过滤标签
  19. tabs: {
  20. type: Array as PropType<string[]>,
  21. default: () => ([]),
  22. },
  23. // 选中的标签
  24. selectedTab: {
  25. type: String,
  26. default: '',
  27. },
  28. })
  29. const { componentMap, getAuthComponent } = useAuth(props.code)
  30. const { onChange } = useAttrs()
  31. const dataList = shallowRef<Ermcp.UserMenu[]>([]) // 数据列表
  32. const dataIndex = shallowRef(0) // 选中的标签
  33. const componentId = computed(() => dataList.value[dataIndex.value]?.code)
  34. const onTabChange = (index: number, { code }: Ermcp.UserMenu) => {
  35. if (onChange instanceof Function) {
  36. onChange(code)
  37. } else {
  38. dataIndex.value = index
  39. }
  40. }
  41. onMounted(() => {
  42. const auth = getAuthComponent();
  43. if (props.tabs.length) {
  44. dataList.value = auth.filter((e) => props.tabs.includes(e.code))
  45. } else {
  46. dataList.value = auth
  47. }
  48. const tabIndex = dataList.value.findIndex((e) => e.code === props.selectedTab)
  49. if (tabIndex > -1) {
  50. dataIndex.value = tabIndex
  51. }
  52. })
  53. </script>