Sidebar.vue 3.6 KB

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