index.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <el-scrollbar :class="['app-sidebar', modelValue && 'is-hide']" view-class="app-sidebar__view">
  3. <div class="app-sidebar__menu">
  4. <el-menu :default-active="$route.name" :collapse="modelValue" @select="routerTo" unique-opened>
  5. <app-submenu :data-list="menus"></app-submenu>
  6. </el-menu>
  7. </div>
  8. <div :class="['app-sidebar__mask', modelValue && 'is-hide']" @click="hideSidebar(true)" v-if="isMobile"></div>
  9. </el-scrollbar>
  10. </template>
  11. <script lang="ts" setup>
  12. import { computed, ref } from 'vue'
  13. import { useRouter } from 'vue-router'
  14. import { useMenu } from '@/hooks/menu'
  15. import { globalState } from '@/store'
  16. import AppSubmenu from '../submenu/index.vue'
  17. const emit = defineEmits(["update:modelValue"]);
  18. defineProps({
  19. // 折叠收起菜单栏
  20. modelValue: Boolean,
  21. })
  22. const { getAuthMenu } = useMenu();
  23. const router = useRouter();
  24. const menus = ref(getAuthMenu());
  25. const hideSidebar = (value: boolean) => {
  26. emit('update:modelValue', value);
  27. }
  28. // 监听设备变化
  29. const isMobile = computed(() => {
  30. const flag = globalState.getValue('isMobile');
  31. if (flag) {
  32. hideSidebar(true);
  33. } else {
  34. hideSidebar(false);
  35. }
  36. return flag;
  37. })
  38. // 菜单跳转
  39. const routerTo = (routerName: string) => {
  40. isMobile.value && hideSidebar(true);
  41. router.replace({ name: routerName });
  42. }
  43. </script>
  44. <style lang="less">
  45. @import './index.less';
  46. </style>