index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div :class="{'has-logo':showLogo}" :style="{ backgroundColor: settings.sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground }">
  3. <logo v-if="showLogo" :collapse="isCollapse" :name="$t('index.name')"/>
  4. <el-scrollbar :class="settings.sideTheme" wrap-class="scrollbar-wrapper">
  5. <el-menu
  6. :default-active="activeMenu"
  7. :collapse="isCollapse"
  8. :background-color="settings.sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground"
  9. :text-color="settings.sideTheme === 'theme-dark' ? variables.menuColor : variables.menuLightColor"
  10. :unique-opened="true"
  11. :active-text-color="settings.theme"
  12. :collapse-transition="false"
  13. mode="vertical"
  14. >
  15. <sidebar-item
  16. v-for="(route, index) in sidebarRouters"
  17. :key="route.path + index"
  18. :item="route"
  19. :base-path="route.path"
  20. />
  21. </el-menu>
  22. </el-scrollbar>
  23. </div>
  24. </template>
  25. <script>
  26. import { mapGetters, mapState } from "vuex";
  27. import Logo from "./Logo";
  28. import SidebarItem from "./SidebarItem";
  29. import variables from "@/assets/styles/variables.scss";
  30. import Bus from '@/utils/Bus.js'
  31. export default {
  32. components: { SidebarItem, Logo },
  33. data() {
  34. return {
  35. language: localStorage.getItem('lang') || 'zh_TW'
  36. }
  37. },
  38. computed: {
  39. ...mapState(["settings"]),
  40. ...mapGetters(["sidebarRouters", "sidebar"]),
  41. activeMenu() {
  42. const route = this.$route;
  43. const { meta, path } = route;
  44. // if set path, the sidebar will highlight the path you set
  45. if (meta.activeMenu) {
  46. return meta.activeMenu;
  47. }
  48. return path;
  49. },
  50. showLogo() {
  51. return this.$store.state.settings.sidebarLogo;
  52. },
  53. variables() {
  54. return variables;
  55. },
  56. isCollapse() {
  57. return !this.sidebar.opened;
  58. }
  59. },
  60. mounted() {
  61. // 监听语言切换事件
  62. Bus.$on('l18zh', (res) => {
  63. this.language = res;
  64. // 强制重新渲染侧边栏
  65. this.$forceUpdate();
  66. })
  67. },
  68. beforeDestroy() {
  69. // 移除事件监听
  70. Bus.$off('l18zh');
  71. }
  72. };
  73. </script>