index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <router-view v-slot="{ Component }">
  3. <transition mode="out-in" :name="theme.pageTransition">
  4. <keep-alive :include="keepAliveNameList" :max="keepAliveMaxNum">
  5. <component :is="Component" :key="routerKey" ref="componentRef" />
  6. </keep-alive>
  7. </transition>
  8. </router-view>
  9. </template>
  10. <script lang="ts" setup>
  11. import { useHead } from '@vueuse/head'
  12. import VabProgress from 'nprogress'
  13. import { keepAliveMaxNum } from '/@/config'
  14. import { useSettingsStore } from '/@/store/modules/settings'
  15. import { useTabsStore } from '/@/store/modules/tabs'
  16. import { handleActivePath } from '/@/utils/routes'
  17. defineOptions({
  18. name: 'VabRouterView',
  19. })
  20. const route = useRoute()
  21. const settingsStore = useSettingsStore()
  22. const { theme } = storeToRefs(settingsStore)
  23. const tabsStore = useTabsStore()
  24. const { getVisitedRoutes: visitedRoutes } = storeToRefs(tabsStore)
  25. const componentRef = ref<any>()
  26. const routerKey = ref<any>()
  27. const keepAliveNameList = ref<any>()
  28. const siteData = reactive<any>({
  29. description: '',
  30. })
  31. useHead({
  32. meta: [
  33. {
  34. name: `description`,
  35. content: computed(() => siteData.description),
  36. },
  37. ],
  38. })
  39. const updateKeepAliveNameList = (refreshRouteName = null) => {
  40. keepAliveNameList.value = visitedRoutes.value
  41. .filter((item) => !item.meta.noKeepAlive && item.name !== refreshRouteName)
  42. .flatMap((item) => item.name)
  43. }
  44. // 更新KeepAlive缓存页面
  45. watchEffect(() => {
  46. routerKey.value = handleActivePath(route, true)
  47. updateKeepAliveNameList()
  48. siteData.description = `${'Vue'} ${'Shop'} ${'Vite'}-${route.meta.title}简介、官网、首页、文档和下载 - 前端开发框架`
  49. })
  50. onBeforeMount(() => {
  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. })
  65. </script>