index.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <script lang="ts" setup>
  2. import { useSettingsStore } from '@/store/modules/settings'
  3. import { useTabsStore } from '@/store/modules/tabs'
  4. import { handleActivePath } from '@/utils/routes'
  5. import { keepAliveMaxNum } from '@/config'
  6. import { useHead } from '@vueuse/head'
  7. import { VabRoute, VabRouteRecord } from '/#/router'
  8. import VabProgress from 'nprogress'
  9. const route: VabRoute = useRoute()
  10. const $sub: any = inject('$sub')
  11. const $unsub: any = inject('$unsub')
  12. const settingsStore = useSettingsStore()
  13. const { theme } = storeToRefs(settingsStore)
  14. const tabsStore = useTabsStore()
  15. const { getVisitedRoutes: visitedRoutes } = storeToRefs(tabsStore)
  16. const componentRef = ref()
  17. const routerKey = ref()
  18. const keepAliveNameList = ref()
  19. const siteData = reactive({
  20. description: '',
  21. })
  22. useHead({
  23. meta: [
  24. {
  25. name: `description`,
  26. content: computed(() => siteData.description),
  27. },
  28. ],
  29. })
  30. const updateKeepAliveNameList = (refreshRouteName = null) => {
  31. keepAliveNameList.value = visitedRoutes.value
  32. .filter(
  33. (item: VabRouteRecord) =>
  34. !item.meta.noKeepAlive && item.name !== refreshRouteName
  35. )
  36. .flatMap((item: VabRouteRecord) => item.name)
  37. }
  38. // 更新KeepAlive缓存页面
  39. watchEffect(() => {
  40. routerKey.value = handleActivePath(route, true)
  41. updateKeepAliveNameList()
  42. siteData.description = `${'Vue'} ${'Admin'} ${'Plus'}-${
  43. route.meta.title
  44. }简介、官网、首页、文档和下载 - 前端开发框架`
  45. })
  46. $sub('get-code', () => {
  47. window.open(
  48. `https://github.com/vue-admin-beautiful/admin-plus/blob/main/${componentRef.value.$options.__source}`
  49. )
  50. })
  51. $sub('reload-router-view', (refreshRouteName: any = route.name) => {
  52. if (theme.value.showProgressBar) VabProgress.start()
  53. const cacheActivePath = routerKey.value
  54. routerKey.value = null
  55. updateKeepAliveNameList(refreshRouteName)
  56. nextTick(() => {
  57. routerKey.value = cacheActivePath
  58. updateKeepAliveNameList()
  59. })
  60. setTimeout(() => {
  61. if (theme.value.showProgressBar) VabProgress.done()
  62. }, 200)
  63. })
  64. onUnmounted(() => {
  65. $unsub('get-code')
  66. $unsub('reload-router-view')
  67. })
  68. </script>
  69. <template>
  70. <router-view v-slot="{ Component }">
  71. <transition
  72. mode="out-in"
  73. :name="theme.showPageTransition ? 'fade-transform' : 'no-transform'"
  74. >
  75. <keep-alive :include="keepAliveNameList" :max="keepAliveMaxNum">
  76. <component :is="Component" :key="routerKey" ref="componentRef" />
  77. </keep-alive>
  78. </transition>
  79. </router-view>
  80. </template>