vue.joedoe.dev

← All the recipesBuilt into Vue

Tabs that keep what you typed

KeepAlive keeps the component instead of destroying it, and onActivated/onDeactivated tell you when it comes back and leaves.

Vue SFC
<script setup lang="ts">
import { onActivated, onDeactivated, ref } from "vue";

/* Inside KeepAlive the component isn't destroyed: it only pauses and resumes. */
const seconds = ref(0);
let timer: ReturnType<typeof setInterval> | undefined;

onActivated(() => (timer = setInterval(() => seconds.value++, 1000)));
onDeactivated(() => clearInterval(timer));
</script>

<template>
	<p>Llevas {{ seconds }} s en esta pestaña (se pausa al salir).</p>
</template>

↑↓ move · ↵ open · esc close