defaults.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <el-container class="layout-container">
  3. <LayoutAside />
  4. <el-container class="layout-container-view h100">
  5. <el-scrollbar ref="layoutScrollbarRef" class="layout-backtop">
  6. <LayoutHeader />
  7. <LayoutMain ref="layoutMainRef" />
  8. </el-scrollbar>
  9. </el-container>
  10. </el-container>
  11. </template>
  12. <script setup lang="ts" name="layoutDefaults">
  13. import { defineAsyncComponent, watch, onMounted, nextTick, ref } from 'vue';
  14. import { useRoute } from 'vue-router';
  15. import { storeToRefs } from 'pinia';
  16. import { useThemeConfig } from '/@/stores/themeConfig';
  17. import { NextLoading } from '/@/utils/loading';
  18. // 引入组件
  19. const LayoutAside = defineAsyncComponent(() => import('/@/layout/component/aside.vue'));
  20. const LayoutHeader = defineAsyncComponent(() => import('/@/layout/component/header.vue'));
  21. const LayoutMain = defineAsyncComponent(() => import('/@/layout/component/main.vue'));
  22. // 定义变量内容
  23. const layoutScrollbarRef = ref<RefType>('');
  24. const layoutMainRef = ref<InstanceType<typeof LayoutMain>>();
  25. const route = useRoute();
  26. const storesThemeConfig = useThemeConfig();
  27. const { themeConfig } = storeToRefs(storesThemeConfig);
  28. // 重置滚动条高度
  29. const updateScrollbar = () => {
  30. // 更新父级 scrollbar
  31. layoutScrollbarRef.value.update();
  32. // 更新子级 scrollbar
  33. layoutMainRef.value!.layoutMainScrollbarRef.update();
  34. };
  35. // 重置滚动条高度,由于组件是异步引入的
  36. const initScrollBarHeight = () => {
  37. nextTick(() => {
  38. setTimeout(() => {
  39. updateScrollbar();
  40. layoutScrollbarRef.value.wrapRef.scrollTop = 0;
  41. layoutMainRef.value!.layoutMainScrollbarRef.wrapRef.scrollTop = 0;
  42. }, 500);
  43. });
  44. };
  45. // 页面加载时
  46. onMounted(() => {
  47. initScrollBarHeight();
  48. NextLoading.done(600);
  49. });
  50. // 监听路由的变化,切换界面时,滚动条置顶
  51. watch(
  52. () => route.path,
  53. () => {
  54. initScrollBarHeight();
  55. }
  56. );
  57. // 监听 themeConfig 配置文件的变化,更新菜单 el-scrollbar 的高度
  58. watch(
  59. themeConfig,
  60. () => {
  61. updateScrollbar();
  62. },
  63. {
  64. deep: true,
  65. }
  66. );
  67. </script>