sideBarItem.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <script setup>
  2. const props = defineProps({
  3. item: {
  4. type: Object,
  5. required: true
  6. }
  7. })
  8. const onlyOneChild = ref(null)
  9. function fatherHasOneSonOrMenu(children = [], parent) {
  10. if (!children) {
  11. children = []
  12. }
  13. const showingChildren = children.filter(item => {
  14. if (item.hidden) {
  15. return false
  16. } else {
  17. onlyOneChild.value = item
  18. return true
  19. }
  20. })
  21. if (showingChildren.length === 1) {
  22. return true
  23. }
  24. if (showingChildren.length === 0) {
  25. onlyOneChild.value = { ...parent, noShowingChildren: true }
  26. return true
  27. }
  28. return false
  29. }
  30. function isExternal(path) {
  31. return /^(https?:|mailto:|tel:)/.test(path)
  32. }
  33. const router = useRouter()
  34. const handleClickMenu = item => {
  35. console.log(item.path)
  36. if (isExternal(item.path)) {
  37. window.open(item.path, '_blank')
  38. } else {
  39. if (item.path.startsWith('/tjmchild')) {
  40. const parts = item.path.split('/') // 使用斜杠分割路径
  41. console.log(parts.slice(3).join('/'))
  42. router.push({
  43. name: parts[2],
  44. params: { page: parts.slice(3).join('/') }
  45. })
  46. } else {
  47. router.push(item.path)
  48. }
  49. }
  50. }
  51. </script>
  52. <template>
  53. <template v-if="!item.hidden">
  54. <template v-if="fatherHasOneSonOrMenu(item.children, item) && !item.alwaysShow">
  55. <el-menu-item :index="onlyOneChild.path" @click="handleClickMenu(onlyOneChild)">
  56. <el-icon size="16">
  57. <component v-if="onlyOneChild.meta.icon.indexOf('ep') !== -1" :is="onlyOneChild.meta.icon.slice(2)" />
  58. <svg-icon v-if="onlyOneChild.meta.icon.indexOf('tjm') !== -1" :icon-class="onlyOneChild.meta.icon.slice(4)" />
  59. </el-icon>
  60. <template #title>{{ onlyOneChild.meta.title }}</template>
  61. </el-menu-item>
  62. </template>
  63. <el-sub-menu v-else :index="item.path">
  64. <template #title>
  65. <el-icon size="16">
  66. <component v-if="item.meta.icon.indexOf('ep') !== -1" :is="item.meta.icon.slice(2)" />
  67. <svg-icon v-if="item.meta.icon.indexOf('tjm') !== -1" :icon-class="item.meta.icon.slice(4)" />
  68. </el-icon>
  69. <span>{{ item.meta.title }}</span>
  70. </template>
  71. <sideBarItem v-for="(route, index) in item.children" :key="route.path + index" :item="route" />
  72. </el-sub-menu>
  73. </template>
  74. </template>
  75. <style lang="scss" scoped></style>