SideBar.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <div class="sidebar">
  3. <logo/>
  4. <a-menu
  5. :openKeys="openKeys"
  6. mode="inline"
  7. @openChange="onOpenChange"
  8. :default-selected-keys="[$route.path.substring(1)]"
  9. >
  10. <template v-for="sidebar in visible(sidebars)">
  11. <a-menu-item v-if="!sidebar.children" :key="sidebar.path"
  12. @click="$router.push('/'+sidebar.path).catch(() => {})">
  13. <a-icon :type="sidebar.meta.icon"/>
  14. <span>{{ sidebar.name }}</span>
  15. </a-menu-item>
  16. <a-sub-menu v-else :key="sidebar.path">
  17. <span slot="title"><a-icon :type="sidebar.meta.icon"/><span>{{ sidebar.name }}</span></span>
  18. <a-menu-item v-for="child in visible(sidebar.children)" :key="child.name">
  19. <router-link :to="'/'+sidebar.path+'/'+child.path">
  20. {{ child.name }}
  21. </router-link>
  22. </a-menu-item>
  23. </a-sub-menu>
  24. </template>
  25. </a-menu>
  26. </div>
  27. </template>
  28. <script>
  29. import Logo from '@/components/Logo/Logo'
  30. export default {
  31. name: 'SideBar',
  32. components: {Logo},
  33. data() {
  34. return {
  35. rootSubmenuKeys: [],
  36. openKeys: [],
  37. sidebars: this.$routeConfig[0]['children']
  38. }
  39. },
  40. created() {
  41. this.sidebars.forEach((element) => {
  42. this.rootSubmenuKeys.push(element)
  43. })
  44. },
  45. methods: {
  46. onOpenChange(openKeys) {
  47. const latestOpenKey = openKeys.find(key => this.openKeys.indexOf(key) === -1)
  48. if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  49. this.openKeys = openKeys
  50. } else {
  51. this.openKeys = latestOpenKey ? [latestOpenKey] : []
  52. }
  53. },
  54. visible(sidebars) {
  55. return sidebars.filter(c => {
  56. return c.meta === undefined || (c.meta.hiddenInSidebar === undefined || c.meta.hiddenInSidebar !== true)
  57. })
  58. }
  59. }
  60. }
  61. </script>
  62. <style lang="less" scoped>
  63. .sidebar {
  64. position: fixed;
  65. width: 200px;
  66. .ant-menu-inline {
  67. height: calc(100vh - 120px);
  68. overflow-y: auto;
  69. overflow-x: hidden;
  70. .ant-menu-item {
  71. width: unset;
  72. }
  73. }
  74. }
  75. .ant-layout-sider-collapsed .logo {
  76. overflow: hidden;
  77. }
  78. .ant-menu-inline, .ant-menu-vertical, .ant-menu-vertical-left {
  79. border-right: unset;
  80. }
  81. </style>