index.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <component :is="layouts[themeConfig.layout]" />
  3. </template>
  4. <script setup lang="ts" name="layout">
  5. import { onBeforeMount, onUnmounted, defineAsyncComponent } from 'vue';
  6. import { storeToRefs } from 'pinia';
  7. import { useThemeConfig } from '/@/stores/themeConfig';
  8. import { Local } from '/@/utils/storage';
  9. import mittBus from '/@/utils/mitt';
  10. // 引入组件
  11. const layouts: any = {
  12. defaults: defineAsyncComponent(() => import('/@/layout/main/defaults.vue')),
  13. classic: defineAsyncComponent(() => import('/@/layout/main/classic.vue')),
  14. transverse: defineAsyncComponent(() => import('/@/layout/main/transverse.vue')),
  15. columns: defineAsyncComponent(() => import('/@/layout/main/columns.vue')),
  16. };
  17. // 定义变量内容
  18. const storesThemeConfig = useThemeConfig();
  19. const { themeConfig } = storeToRefs(storesThemeConfig);
  20. // 20240117 最大窗体宽度
  21. let maxClientWidth = document.body.clientWidth;
  22. // 窗口大小改变时(适配移动端)
  23. const onLayoutResize = () => {
  24. if (!Local.get('oldLayout')) Local.set('oldLayout', themeConfig.value.layout);
  25. const clientWidth = document.body.clientWidth;
  26. // 20240117 最大窗体宽度 > 当前宽度,不触发 layoutMobileResize 事件
  27. if (maxClientWidth > clientWidth) return;
  28. maxClientWidth = clientWidth;
  29. if (clientWidth < 1000) {
  30. themeConfig.value.isCollapse = false;
  31. mittBus.emit('layoutMobileResize', {
  32. layout: 'defaults',
  33. clientWidth,
  34. });
  35. } else {
  36. mittBus.emit('layoutMobileResize', {
  37. layout: Local.get('oldLayout') ? Local.get('oldLayout') : themeConfig.value.layout,
  38. clientWidth,
  39. });
  40. }
  41. };
  42. // 页面加载前
  43. onBeforeMount(() => {
  44. onLayoutResize();
  45. window.addEventListener('resize', onLayoutResize);
  46. });
  47. // 页面卸载时
  48. onUnmounted(() => {
  49. window.removeEventListener('resize', onLayoutResize);
  50. });
  51. </script>