Sidebar.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="sidebar">
  3. <!-- background-color="#07c4a8" text-color="#666" active-text-color="#fff" -->
  4. <el-menu
  5. class="sidebar-el-menu"
  6. :default-active="onRoutes"
  7. :collapse="collapse"
  8. background-color="#324157"
  9. text-color="#bfcbd9"
  10. active-text-color="#20a0ff"
  11. unique-opened
  12. router
  13. >
  14. <template v-for="item in items">
  15. <template v-if="item.subs">
  16. <el-submenu class="second" :index="item.index" :key="item.index">
  17. <template slot="title">
  18. <i :class="item.icon"></i>
  19. <span slot="title">{{ item.title }}</span>
  20. </template>
  21. <template v-for="subItem in item.subs">
  22. <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
  23. <template slot="title">{{ subItem.title }}</template>
  24. <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">{{ threeItem.title }}</el-menu-item>
  25. </el-submenu>
  26. <el-menu-item v-else :index="subItem.index" :key="subItem.index">{{ subItem.title }}</el-menu-item>
  27. </template>
  28. </el-submenu>
  29. </template>
  30. <template v-else>
  31. <el-menu-item class="first" :index="item.index" :key="item.index">
  32. <i :class="item.icon"></i>
  33. <span slot="title">{{ item.title }}</span>
  34. </el-menu-item>
  35. </template>
  36. </template>
  37. </el-menu>
  38. </div>
  39. </template>
  40. <script>
  41. import _ from 'lodash';
  42. import { mapState, createNamespacedHelpers } from 'vuex';
  43. import bus from '../common/bus';
  44. export default {
  45. data() {
  46. return {
  47. collapse: false,
  48. items: [
  49. {
  50. icon: 'el-icon-s-home',
  51. index: 'homeIndex',
  52. title: '系统首页',
  53. },
  54. {
  55. icon: 'el-icon-s-home',
  56. index: 'company',
  57. title: '企业信息',
  58. },
  59. ],
  60. };
  61. },
  62. computed: {
  63. ...mapState(['user']),
  64. onRoutes() {
  65. return this.$route.path.replace('/', '');
  66. },
  67. },
  68. created() {
  69. // 通过 Event Bus 进行组件间通信,来折叠侧边栏
  70. bus.$on('collapse', msg => {
  71. this.collapse = msg;
  72. bus.$emit('collapse-content', msg);
  73. });
  74. },
  75. methods: {
  76. // 分配用户彩带权限
  77. getMenu() {
  78. // 用户信息
  79. let user = this.user;
  80. // 复制列表
  81. let list = _.cloneDeep(this.items);
  82. let data = [];
  83. list.push(...data);
  84. this.$set(this, `items`, _.uniqBy(list, 'index'));
  85. },
  86. },
  87. watch: {
  88. user: {
  89. deep: true,
  90. immediate: true,
  91. handler(val) {
  92. this.getMenu();
  93. },
  94. },
  95. },
  96. };
  97. </script>
  98. <style lang="less" scoped>
  99. .sidebar {
  100. display: block;
  101. position: absolute;
  102. left: 0;
  103. top: 70px;
  104. bottom: 0;
  105. overflow-y: scroll;
  106. }
  107. .sidebar::-webkit-scrollbar {
  108. width: 0;
  109. }
  110. .sidebar-el-menu:not(.el-menu--collapse) {
  111. width: 250px;
  112. }
  113. .sidebar > ul {
  114. height: 100%;
  115. }
  116. // /deep/.first {
  117. // padding-left: 0 !important;
  118. // }
  119. // /deep/.second .el-submenu__title {
  120. // padding-left: 0 !important;
  121. // }
  122. </style>