wujingjing
2024-12-11 3229f6a34f4982d1a551d86e68d313482e33f701
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<template>
    <el-container class="layout-container flex-column">
        <LayoutHeader />
        <el-container class="layout-mian-height">
            <LayoutAside />
            <div class="flex-column layout-backtop">
                <LayoutTagsView v-if="isTagsview" />
                <LayoutMain ref="layoutMainRef" />
            </div>
        </el-container>
    </el-container>
</template>
 
<script setup lang="ts" name="layoutClassic">
import { defineAsyncComponent, computed, ref, watch, nextTick, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
 
// 引入组件
const LayoutAside = defineAsyncComponent(() => import('/@/layout/component/aside.vue'));
const LayoutHeader = defineAsyncComponent(() => import('/@/layout/component/header.vue'));
const LayoutMain = defineAsyncComponent(() => import('/@/layout/component/main.vue'));
const LayoutTagsView = defineAsyncComponent(() => import('/@/layout/navBars/tagsView/tagsView.vue'));
 
// 定义变量内容
const layoutMainRef = ref<InstanceType<typeof LayoutMain>>();
const route = useRoute();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
 
// 判断是否显示 tasgview
const isTagsview = computed(() => {
    return themeConfig.value.isTagsview;
});
// 重置滚动条高度,更新子级 scrollbar
const updateScrollbar = () => {
    layoutMainRef.value?.layoutMainScrollbarRef.update();
};
// 重置滚动条高度,由于组件是异步引入的
const initScrollBarHeight = () => {
    nextTick(() => {
        setTimeout(() => {
            updateScrollbar();
            // '!' not null 断言操作符,不执行运行时检查
            if (layoutMainRef.value) layoutMainRef.value!.layoutMainScrollbarRef.wrapRef.scrollTop = 0;
        }, 500);
    });
};
// 页面加载时
onMounted(() => {
    initScrollBarHeight();
});
// 监听路由的变化,切换界面时,滚动条置顶
watch(
    () => route.path,
    () => {
        initScrollBarHeight();
    }
);
// 监听 themeConfig 配置文件的变化,更新菜单 el-scrollbar 的高度
watch(
    themeConfig,
    () => {
        updateScrollbar();
    },
    {
        deep: true,
    }
);
</script>