Skip to content

How to use keep-alive to cache nested route pages #2406

Closed Answered by tbontb-iaq
tbontb-iaq asked this question in Help and Questions
Discussion options

You must be logged in to vote

Found one solution, here is code, maybe more boundary conditions need to be considered:

<template lang="pug">
router-view(#='{ Component, route }')
  template(v-if='Component')
    transition(mode='out-in')
      keep-alive
        component(:is='Component', :key='getKey(route, Component)')
</template>

<script setup lang="ts">
import { pathToRegexp } from 'path-to-regexp'
import { type RouteLocationNormalizedLoaded } from 'vue-router'

function getKey(route: RouteLocationNormalizedLoaded, Component: VNode) {
  const matched = route.matched.find(
    m => m.components?.default === Component.type
  )
  if (!matched) return route.path

  const { regexp } = pathToRegexp(matched.path, { end: f…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tbontb-iaq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant