transverse.vue 1.5 KB

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