Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

522
Vistas
How to use keepalive in Nuxt 3?

I want to learn more about Nuxt 3 and got a basic template to test keepalive:

problem

My test.vue-component starts everytime with a falsy value, which triggers the setTimeout.

expectations

I expected the timeout method to be executed only once and nuxt will cache the values after a route change. So the timeout method should not start again.

folder structure

  • /components
    • test.vue
  • /layouts
    • default.vue
  • /pages
    • /item
      • [id].vue
    • index.vue

approach

// index.vue
<script setup>
definePageMeta({
  keepalive: true
})
</script>

<template>
  <div>
    <h1>Home</h1>
    <NuxtLink to="/item/1">to item 1</NuxtLink>
    <hr>
    <Test /> /* <- keep me alive after route changes */
  </div>
</template>
// [id].vue
// nothing here, just to navigate back to the root page

<template>
  <div>
    <h1>Item page</h1>
    <NuxtLink to="/">back to home</NuxtLink>
  </div>
</template>
// test.vue
<script setup>
definePageMeta({
  keepalive: true
})

const showText = $ref(false)
onMounted(() => {
  if (showText) return
  setTimeout(() => {
    showText= true
  }, 1000);
})
</script>

<template>
  <div>
    <p>test:</p>
    <div v-if="showText">  /* is always false after route changes */
      <p>hello world, should be alive?!</p>
    </div>
  </div>
</template>

The documentation says:

Nuxt will automatically wrap your page in the Vue component if you set keepalive: true in your definePageMeta. [...]

questions

  • How would keepalive work in my example?

  • Do I have to declare the keepalive both in the parent and the child component?

  • [Optional]: What should I do, to keep the parent (index.vue) alive?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda