sider.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <a-row>
  3. <a-col>
  4. <a-layout-sider
  5. :trigger="null"
  6. v-model:collapsed="collapsed"
  7. collapsible
  8. style="background: #fff; height: 95%;"
  9. breakpoint="lg"
  10. @collapse="onCollapse"
  11. @breakpoint="onBreakpoint"
  12. >
  13. <a-menu
  14. :selectedKeys="selectedKeys2"
  15. v-model:openKeys="openKeys"
  16. mode="inline"
  17. :style="{ height: '100%', borderRight: 0 }"
  18. >
  19. <template v-for="item in appRouter.router.sider.route" :key="item.path">
  20. <a-menu-item v-if="!item.children" :key="item.path" @click="changeRoute(item)" >
  21. <user-outlined />
  22. <span>{{item.name}}</span>
  23. </a-menu-item>
  24. <a-sub-menu v-else >
  25. <template #title>
  26. <user-outlined />
  27. <span>
  28. {{item.name}}
  29. </span>
  30. </template>
  31. <a-menu-item @click="changeRoute(_)" v-for="_ in item.children" :key="_.path" >
  32. <user-outlined />{{_.name}}
  33. </a-menu-item>
  34. </a-sub-menu>
  35. </template>
  36. </a-menu>
  37. </a-layout-sider>
  38. <div class="control" >
  39. <menu-fold-outlined @click="collapsed = true" v-if="!collapsed" style="font-size: 26px;cursor: pointer;" />
  40. <menu-unfold-outlined @click="collapsed = false" v-else style="font-size: 26px;cursor: pointer;" />
  41. </div>
  42. </a-col>
  43. </a-row>
  44. </template>
  45. <script lang="ts" setup >
  46. import { ref } from 'vue'
  47. import RootRouter from '@/router/index'
  48. import { useAppRouter } from '@/store/router'
  49. import { useEmitter } from '@/hooks/index'
  50. import { Emitter } from '@/enum/emitter'
  51. import { MenuFoldOutlined, MenuUnfoldOutlined, UserOutlined } from '@ant-design/icons-vue'
  52. const emitter = useEmitter()
  53. const appRouter = useAppRouter()
  54. console.log(appRouter)
  55. const onCollapse = () => {}
  56. const onBreakpoint = () => {}
  57. const collapsed = ref<boolean>(false)
  58. const selectedKeys2 = ref<string[]>([appRouter.router.sider!.selectPath])
  59. const openKeys = ref<string[]>(appRouter.router.sider!.openKeys)
  60. emitter.on(Emitter.NAVBAR, () => {
  61. selectedKeys2.value = [appRouter.router.sider!.selectPath]
  62. openKeys.value = appRouter.router.sider!.openKeys
  63. })
  64. const changeRoute = (route: Router.RoutesProps) => {
  65. RootRouter.push(route.path)
  66. }
  67. const hasOneShowingChild = (children = [], parent) => {
  68. const showingChildren = children.filter(item => {
  69. if (item.hidden) {
  70. return false
  71. } else {
  72. return true
  73. }
  74. })
  75. if (showingChildren.length === 1) {
  76. return true
  77. }
  78. if (showingChildren.length === 0) {
  79. return true
  80. }
  81. return false
  82. }
  83. </script>
  84. <style lang="less" scoped >
  85. .control {
  86. width: 100%;
  87. height: 5%;
  88. background-color: #fff;
  89. display: flex;
  90. justify-content: center;
  91. align-items: center;
  92. }
  93. /deep/ .ant-layout-sider-children {
  94. margin-top: -4px;
  95. }
  96. </style>