Keep-alive helps Vue remember a component's state and avoid reloading it every time you switch views. This saves time and makes your app feel faster.
Keep-alive for expensive components in Vue
<keep-alive> <component-to-cache /> </keep-alive>
MyExpensiveComponent so it won't reload when toggled.<keep-alive> <MyExpensiveComponent /> </keep-alive>
CompA and CompB when used dynamically.<keep-alive include="CompA,CompB"> <component :is="currentComp" /> </keep-alive>
CompC.<keep-alive exclude="CompC"> <component :is="currentComp" /> </keep-alive>
This example shows two components, A and B, each displaying the time they loaded. Using <keep-alive> keeps the component state, so switching back to a component shows the original load time instead of updating it.
<template>
<div>
<button @click="current = 'CompA'">Show A</button>
<button @click="current = 'CompB'">Show B</button>
<keep-alive>
<component :is="current" />
</keep-alive>
</div>
</template>
<script setup>
import { ref, defineComponent } from 'vue'
const current = ref('CompA')
const CompA = defineComponent({
name: 'CompA',
data() {
return { time: new Date().toLocaleTimeString() }
},
template: `<div>Component A - Loaded at: {{ time }}</div>`
})
const CompB = defineComponent({
name: 'CompB',
data() {
return { time: new Date().toLocaleTimeString() }
},
template: `<div>Component B - Loaded at: {{ time }}</div>`
})
</script>Keep-alive only caches components that are dynamically rendered using <component :is="..." /> or conditional rendering.
Cached components keep their state, including data and DOM, but you can control cache size with max prop.
Use include and exclude props to control which components to cache by name.
Keep-alive caches components to avoid reloading and improve speed.
Wrap dynamic components inside <keep-alive> to keep their state.
Use include and exclude to control caching behavior.
Practice
<keep-alive> in Vue?Solution
Step 1: Understand what
<keep-alive>does<keep-alive>caches components so they don't reload when toggled.Step 2: Compare options to this behavior
Only 'To cache components and preserve their state when toggled' describes caching and preserving state, which is the main purpose.Final Answer:
To cache components and preserve their state when toggled -> Option AQuick Check:
Keep-alive caches components [OK]
- Thinking keep-alive removes components
- Confusing keep-alive with styling or animations
- Assuming keep-alive forces reloads
keep-alive in Vue 3?Solution
Step 1: Recall Vue 3 syntax for keep-alive
The<keep-alive>tag wraps the dynamic<component>with:isbinding.Step 2: Check each option's correctness
<keep-alive><component :is="currentView" /></keep-alive>correctly wraps<component :is="currentView" />inside<keep-alive>. Others misuse attributes or tags.Final Answer:
<keep-alive><component :is="currentView" /></keep-alive> -> Option AQuick Check:
Wrap dynamic component inside keep-alive tags [OK]
- Using keep-alive as a self-closing tag with :is
- Adding keep-alive as a prop on component
- Nesting currentView as a tag inside component
<keep-alive include="CompA,CompB"> <component :is="currentComp" /> </keep-alive>
If
currentComp changes from 'CompA' to 'CompC' and back to 'CompA', what happens to the state of 'CompA'?Solution
Step 1: Understand include prop effect
Theincludeattribute caches only components named 'CompA' and 'CompB'.Step 2: Analyze component switching
Switching to 'CompC' unloads it (not cached). Returning to 'CompA' restores cached state.Final Answer:
State is preserved because 'CompA' is included in keep-alive -> Option CQuick Check:
Include caches listed components [OK]
- Assuming all components are cached regardless of include
- Thinking state resets on every switch
- Confusing include with exclude
<keep-alive exclude="CompB"> <component :is="currentComp" /> </keep-alive>
When switching from 'CompA' to 'CompB', the state of 'CompA' is lost. Why?
Solution
Step 1: Understand exclude behavior
Theexcludeattribute prevents caching only for 'CompB'. Other components like 'CompA' remain cached.Step 2: Analyze why 'CompA' state is lost
If 'CompA' state is lost, likely the component is unmounted or recreated outside keep-alive scope, not due to exclude.Final Answer:
Because 'CompB' is excluded, only 'CompB' is not cached; 'CompA' should keep state -> Option BQuick Check:
Exclude only skips caching excluded components [OK]
- Thinking exclude disables caching for all components
- Assuming exclude destroys other cached components
- Confusing exclude with disabling keep-alive
CompX and CompY. You want to cache CompX but not CompY. Which is the best way to use <keep-alive> to achieve this?Solution
Step 1: Identify caching goal
You want to cache onlyCompX, notCompY.Step 2: Use include to cache specific components
Settinginclude="CompX"caches onlyCompX.CompYwon't be cached.Step 3: Check other options
ExcludeCompXcaches everything exceptCompX(wrong). IncludeCompYcaches onlyCompY(wrong). No filters caches all (wrong).Final Answer:
<keep-alive include="CompX"><component :is="currentComp" /></keep-alive> -> Option DQuick Check:
Include caches only listed components [OK]
- Using exclude to cache only one component
- Including the wrong component name
- Not using include or exclude and caching all
Ready to prove it?
Pass the test and this pattern moves unknown
