Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep-alive doesn't work with nested router-view #8369

Open
haiting opened this issue May 19, 2023 · 2 comments
Open

Keep-alive doesn't work with nested router-view #8369

haiting opened this issue May 19, 2023 · 2 comments

Comments

@haiting
Copy link

haiting commented May 19, 2023

Vue version

3.2.47

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-pzruhm?file=src%2Fviews%2FTestView.vue

Steps to reproduce

  • use keep-alive cache the component
    BlankView.vue
 <router-view #="{ Component }" v-if="isRouterAlive">
        <keep-alive>
          <component :is="Component" style="margin: 0 16px" />
        </keep-alive>
      </router-view>
  • And the Level 3 routes: basl and bash have common parent components
    TestView.vue
<template>
  <router-view></router-view>
</template>

<script>
export default {
  name: 'TestMyLayout'
}
</script>
  • router:
routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/test',
      component: BlankView,
      children: [
        {
          path: 'test-1',
          component: TestView,
          children: [
            {
              path: 'example',
              component: TestView,
              children: [
                {
                  path: 'basl',
                  name: 'Basl',
                  component: () => import('../views/example/basl/index.vue'),
                  meta: {
                    title: 'basl title'
                  }
                },
                {
                  path: 'bash',
                  name: 'Bash',
                  component: () => import('../views/example/bash/index.vue'),
                  meta: {
                    title: 'bash title'
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]

What is expected?

Cache is working for The Level 3 routes

What is actually happening?

Cache is not working for The Level 3 routes

System Info

No response

Any additional comments?

No response

@Dchih
Copy link

Dchih commented Sep 20, 2024

You can use

<router-view #="{ Component }">
        <keep-alive>
          <component :is="Component" />
        </keep-alive>
 </router-view>

in your Level 3 router view to fix this problem.
but it comes another problem that every level 3 component will invoke onMounted twice
and I don't find the case in keepalive testing cases
should I open another issue to report this problem ?

@edison1105
Copy link
Member

@Dchih Please feel free to create a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants