56 lines
2.4 KiB
Vue
Executable File
56 lines
2.4 KiB
Vue
Executable File
<template>
|
|
<main class="main-wrap h-full bg-page">
|
|
<el-scrollbar>
|
|
<div class="admin-content px-2 py-4">
|
|
<router-view v-slot="{ Component, route: viewRoute }">
|
|
<keep-alive :include="includeList" :max="20">
|
|
<component v-if="Component && isRouteShow && currentView.shell" :is="currentView.shell" :key="currentView.id">
|
|
<component :is="Component" :key="viewRoute.fullPath" />
|
|
</component>
|
|
<component v-else-if="Component && isRouteShow" :is="Component" :key="viewRoute.fullPath" />
|
|
</keep-alive>
|
|
</router-view>
|
|
</div>
|
|
</el-scrollbar>
|
|
</main>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, defineComponent, watch, type Component } from 'vue'
|
|
|
|
import useAppStore from '@/stores/modules/app'
|
|
import useTabsStore from '@/stores/modules/multipleTabs'
|
|
import useSettingStore from '@/stores/modules/setting'
|
|
|
|
const appStore = useAppStore()
|
|
const tabs = useTabsStore()
|
|
const settings = useSettingStore()
|
|
const route = useRoute()
|
|
const isRouteShow = computed(() => appStore.isRouteShow)
|
|
|
|
// KeepAlive includes/excludes component names, not URLs. Give each opted-in
|
|
// work tab its own transparent, named cache boundary so removing one page
|
|
// cannot also evict another page backed by the same Vue component.
|
|
const cacheName = (id: number) => 'AdminCachedView' + id
|
|
const shells = new Map<number, Component>()
|
|
const openIds = computed(() => tabs.tabList.flatMap(tab => tab.cacheId ? [tab.cacheId] : []))
|
|
const includeList = computed(() => settings.openMultipleTabs ? openIds.value.map(cacheName) : [])
|
|
watch(openIds, ids => {
|
|
for (const id of shells.keys()) if (!ids.includes(id)) shells.delete(id)
|
|
})
|
|
const currentView = computed<{ path: string; id?: number; shell?: Component }>((previous) => {
|
|
const path = route.fullPath
|
|
const id = tabs.tabList.find(item => item.fullPath === path)?.cacheId
|
|
// While the router is leaving a just-closed tab, don't mount a throwaway
|
|
// copy of its business page. KeepAlive will prune it after the route commit.
|
|
if (!id) return previous?.path === path ? previous : { path }
|
|
if (!shells.has(id)) {
|
|
shells.set(id, defineComponent({
|
|
name: cacheName(id),
|
|
setup: (_, { slots }) => () => slots.default?.()[0] ?? null
|
|
}))
|
|
}
|
|
return { path, id, shell: shells.get(id) }
|
|
})
|
|
</script>
|