columns.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <el-container class="layout-container">
  3. <ColumnsAside />
  4. <el-container class="layout-columns-warp layout-container-view h100">
  5. <LayoutAside />
  6. <el-scrollbar ref="layoutScrollbarRef" class="layout-backtop">
  7. <LayoutHeader />
  8. <LayoutMain ref="layoutMainRef" />
  9. </el-scrollbar>
  10. </el-container>
  11. </el-container>
  12. </template>
  13. <script setup lang="ts" name="layoutColumns">
  14. import { defineAsyncComponent, watch, onMounted, nextTick, ref } from 'vue';
  15. import { useRoute } from 'vue-router';
  16. import { storeToRefs } from 'pinia';
  17. import { useThemeConfig } from '/@/stores/themeConfig';
  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. const ColumnsAside = defineAsyncComponent(() => import('/@/layout/component/columnsAside.vue'));
  23. // 定义变量内容
  24. const layoutScrollbarRef = ref<RefType>('');
  25. const layoutMainRef = ref<InstanceType<typeof LayoutMain>>();
  26. const route = useRoute();
  27. const storesThemeConfig = useThemeConfig();
  28. const { themeConfig } = storeToRefs(storesThemeConfig);
  29. // 重置滚动条高度
  30. const updateScrollbar = () => {
  31. // 更新父级 scrollbar
  32. layoutScrollbarRef.value.update();
  33. // 更新子级 scrollbar
  34. layoutMainRef.value && layoutMainRef.value!.layoutMainScrollbarRef.update();
  35. };
  36. // 重置滚动条高度,由于组件是异步引入的
  37. const initScrollBarHeight = () => {
  38. nextTick(() => {
  39. setTimeout(() => {
  40. updateScrollbar();
  41. layoutScrollbarRef.value.wrapRef.scrollTop = 0;
  42. layoutMainRef.value!.layoutMainScrollbarRef.wrapRef.scrollTop = 0;
  43. }, 500);
  44. });
  45. };
  46. // 页面加载时
  47. onMounted(() => {
  48. initScrollBarHeight();
  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>