index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!-- 动态组件 -->
  2. <template>
  3. <div :class="['app-auth-component', direction]">
  4. <div class="auth-navbar">
  5. <ul class="tabs" v-if="dataList.length">
  6. <li :class="['tabs-item', item.code === componentId && 'is-active']" v-for="(item, index) in dataList"
  7. :key="index" @click="onTabChange(item.code)">
  8. {{ item.title }}
  9. </li>
  10. </ul>
  11. <slot></slot>
  12. </div>
  13. <div class="auth-container">
  14. <component :is="componentMap.get(componentId)" :code="componentId" v-bind="options" v-if="componentId" />
  15. </div>
  16. </div>
  17. </template>
  18. <script lang="ts" setup>
  19. import { shallowRef, PropType, onMounted, useAttrs } from 'vue'
  20. import { useAuth } from '@/hooks/auth'
  21. const props = defineProps({
  22. code: String,
  23. options: Object,
  24. // 组件方向
  25. direction: {
  26. type: String as PropType<'top' | 'bottom' | 'left' | 'right'>,
  27. default: 'top',
  28. },
  29. // 过滤标签
  30. tabs: {
  31. type: Array as PropType<string[]>,
  32. default: () => ([]),
  33. },
  34. // 选中的标签
  35. selectedTab: {
  36. type: String,
  37. default: '',
  38. },
  39. })
  40. const { componentMap, getAuthComponent } = useAuth(props.code)
  41. const { onChange } = useAttrs()
  42. const componentId = shallowRef<string>()
  43. const dataList = shallowRef<Ermcp.UserMenu[]>([]) // 数据列表
  44. const onTabChange = (code: string) => {
  45. if (componentId.value !== code) {
  46. if (onChange instanceof Function) {
  47. onChange(code)
  48. } else {
  49. componentId.value = code
  50. }
  51. }
  52. }
  53. onMounted(() => {
  54. const auth = getAuthComponent();
  55. if (props.tabs.length) {
  56. dataList.value = auth.filter((e) => props.tabs.includes(e.code))
  57. } else {
  58. dataList.value = auth
  59. }
  60. const tabItem = dataList.value.find((e) => e.code === props.selectedTab)
  61. if (tabItem) {
  62. componentId.value = tabItem.code
  63. } else {
  64. componentId.value = dataList.value[0]?.code
  65. }
  66. })
  67. </script>
  68. <style lang="less">
  69. @import './index.less';
  70. </style>