Files
admin-govern/src/layouts/admin/router-view/main.vue

105 lines
3.0 KiB
Vue
Raw Normal View History

2023-12-21 16:42:39 +08:00
<template>
2024-01-19 14:08:07 +08:00
<el-main class="layout-main" :style="mainHeight()">
<router-view v-slot="{ Component }">
<transition :name="config.layout.mainAnimation" mode="out-in">
<keep-alive :include="state.keepAliveComponentNameList">
<component :is="Component" :key="state.componentKey" />
</keep-alive>
</transition>
</router-view>
2023-12-21 16:42:39 +08:00
</el-main>
</template>
2024-01-19 14:08:07 +08:00
<script setup lang="ts">
2023-12-21 16:42:39 +08:00
import { ref, reactive, onMounted, watch, onBeforeMount, onUnmounted, nextTick, provide } from 'vue'
import { useRoute, type RouteLocationNormalized } from 'vue-router'
2024-01-19 14:08:07 +08:00
import { mainHeight } from '@/utils/layout'
2023-12-21 16:42:39 +08:00
import useCurrentInstance from '@/utils/useCurrentInstance'
import { useConfig } from '@/stores/config'
import { useNavTabs } from '@/stores/navTabs'
defineOptions({
2023-12-22 10:22:22 +08:00
name: 'layout/main'
2023-12-21 16:42:39 +08:00
})
const { proxy } = useCurrentInstance()
const route = useRoute()
const config = useConfig()
const navTabs = useNavTabs()
const state: {
componentKey: string
keepAliveComponentNameList: string[]
} = reactive({
componentKey: route.path,
2023-12-22 10:22:22 +08:00
keepAliveComponentNameList: []
2023-12-21 16:42:39 +08:00
})
2024-01-19 14:08:07 +08:00
const addKeepAliveComponentName = function (keepAliveName: string | undefined) {
2023-12-21 16:42:39 +08:00
if (keepAliveName) {
let exist = state.keepAliveComponentNameList.find((name: string) => {
return name === keepAliveName
})
if (exist) return
state.keepAliveComponentNameList.push(keepAliveName)
}
}
onBeforeMount(() => {
proxy.eventBus.on('onTabViewRefresh', (menu: RouteLocationNormalized) => {
2024-01-19 14:08:07 +08:00
state.keepAliveComponentNameList = state.keepAliveComponentNameList.filter(
(name: string) => menu.meta.keepalive !== name
)
2023-12-21 16:42:39 +08:00
state.componentKey = ''
nextTick(() => {
state.componentKey = menu.path
addKeepAliveComponentName(menu.meta.keepalive as string)
})
})
proxy.eventBus.on('onTabViewClose', (menu: RouteLocationNormalized) => {
2024-01-19 14:08:07 +08:00
state.keepAliveComponentNameList = state.keepAliveComponentNameList.filter(
(name: string) => menu.meta.keepalive !== name
)
2023-12-21 16:42:39 +08:00
})
})
onUnmounted(() => {
proxy.eventBus.off('onTabViewRefresh')
proxy.eventBus.off('onTabViewClose')
})
onMounted(() => {
// 确保刷新页面时也能正确取得当前路由 keepalive 参数
if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') {
addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive)
}
})
watch(
() => route.path,
() => {
state.componentKey = route.path
if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') {
addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive)
}
}
)
</script>
2024-01-19 14:08:07 +08:00
<style scoped lang="scss">
2023-12-21 16:42:39 +08:00
.layout-container .layout-main {
padding: 0 !important;
overflow: hidden;
width: 100%;
height: 100%;
}
2023-12-22 10:22:22 +08:00
2023-12-21 16:42:39 +08:00
.layout-main-scrollbar {
width: 100%;
position: relative;
//overflow: hidden;
2023-12-21 16:42:39 +08:00
}
</style>