index.vue 2.8 KB

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