Bird
Raised Fist0
Vueframework~10 mins

Keep-alive for expensive components in Vue - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to wrap the component with keep-alive.

Vue
<template>
  <[1]>
    <ExpensiveComponent />
  </[1]>
</template>
Drag options to blanks, or click blank then click option'
Atransition
Bkeep-alive
Csuspense
Dteleport
Attempts:
3 left
💡 Hint
Common Mistakes
Using instead of causes no caching.
Using is for async components, not caching.
Using moves DOM nodes, not caching.
2fill in blank
medium

Complete the code to conditionally cache the component only when active.

Vue
<template>
  <keep-alive :[1]="isActive">
    <ExpensiveComponent />
  </keep-alive>
</template>
Drag options to blanks, or click blank then click option'
Amax
Bexclude
Cinclude
Ddisabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using include or exclude filters components by name, not boolean.
Using max is for limiting cache size, not enabling/disabling caching.
3fill in blank
hard

Fix the error in the code to properly cache only components named 'ExpensiveComponent'.

Vue
<template>
  <keep-alive include="[1]">
    <component :is="currentComponent" />
  </keep-alive>
</template>
Drag options to blanks, or click blank then click option'
A"ExpensiveComponent"
BExpensiveComponent
C'ExpensiveComponent'
D['ExpensiveComponent']
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted names causes Vue to look for variables.
Using single quotes inside double quotes breaks HTML attribute syntax.
Passing an array is invalid for the include prop.
4fill in blank
hard

Fill both blanks to cache components named 'ExpensiveComponent' and exclude 'CheapComponent'.

Vue
<template>
  <keep-alive include=[1] exclude=[2]>
    <component :is="currentComponent" />
  </keep-alive>
</template>
Drag options to blanks, or click blank then click option'
A"ExpensiveComponent"
B"CheapComponent"
C'CheapComponent'
D'ExpensiveComponent'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes inside attributes breaks HTML syntax.
Swapping include and exclude values causes wrong caching behavior.
5fill in blank
hard

Fill all three blanks to cache components named 'ExpensiveComponent', enable caching conditionally, and exclude 'TemporaryComponent'.

Vue
<template>
  <keep-alive include=[1] :max=[2] exclude=[3]>
    <component :is="currentComponent" />
  </keep-alive>
</template>
Drag options to blanks, or click blank then click option'
A"ExpensiveComponent"
BisCachingActive
C"TemporaryComponent"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted component names causes errors.
Passing a string instead of a boolean for max causes runtime issues.
Mixing up include and exclude values.

Practice

(1/5)
1. What is the main purpose of using <keep-alive> in Vue?
easy
A. To cache components and preserve their state when toggled
B. To permanently remove components from the DOM
C. To force components to reload every time they are shown
D. To style components with CSS animations

Solution

  1. Step 1: Understand what <keep-alive> does

    <keep-alive> caches components so they don't reload when toggled.
  2. 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.
  3. Final Answer:

    To cache components and preserve their state when toggled -> Option A
  4. Quick Check:

    Keep-alive caches components [OK]
Hint: Remember: keep-alive means keep component state alive [OK]
Common Mistakes:
  • Thinking keep-alive removes components
  • Confusing keep-alive with styling or animations
  • Assuming keep-alive forces reloads
2. Which of the following is the correct syntax to wrap a dynamic component with keep-alive in Vue 3?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall Vue 3 syntax for keep-alive

    The <keep-alive> tag wraps the dynamic <component> with :is binding.
  2. 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.
  3. Final Answer:

    <keep-alive><component :is="currentView" /></keep-alive> -> Option A
  4. Quick Check:

    Wrap dynamic component inside keep-alive tags [OK]
Hint: Wrap inside tags [OK]
Common Mistakes:
  • 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
3. Given this Vue template:
<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'?
medium
A. State is preserved only if 'CompC' is also included
B. State is lost because 'CompA' is not included in keep-alive
C. State is preserved because 'CompA' is included in keep-alive
D. State is always reset on component switch

Solution

  1. Step 1: Understand include prop effect

    The include attribute caches only components named 'CompA' and 'CompB'.
  2. Step 2: Analyze component switching

    Switching to 'CompC' unloads it (not cached). Returning to 'CompA' restores cached state.
  3. Final Answer:

    State is preserved because 'CompA' is included in keep-alive -> Option C
  4. Quick Check:

    Include caches listed components [OK]
Hint: Only included components keep state cached [OK]
Common Mistakes:
  • Assuming all components are cached regardless of include
  • Thinking state resets on every switch
  • Confusing include with exclude
4. Consider this Vue code snippet:
<keep-alive exclude="CompB">
  <component :is="currentComp" />
</keep-alive>

When switching from 'CompA' to 'CompB', the state of 'CompA' is lost. Why?
medium
A. Because 'CompB' is excluded, switching to it destroys cached components including 'CompA'
B. Because 'CompB' is excluded, only 'CompB' is not cached; 'CompA' should keep state
C. Because 'CompB' is excluded, keep-alive disables caching for all components
D. Because exclude disables keep-alive entirely

Solution

  1. Step 1: Understand exclude behavior

    The exclude attribute prevents caching only for 'CompB'. Other components like 'CompA' remain cached.
  2. 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.
  3. Final Answer:

    Because 'CompB' is excluded, only 'CompB' is not cached; 'CompA' should keep state -> Option B
  4. Quick Check:

    Exclude only skips caching excluded components [OK]
Hint: Exclude skips caching only for listed components [OK]
Common Mistakes:
  • Thinking exclude disables caching for all components
  • Assuming exclude destroys other cached components
  • Confusing exclude with disabling keep-alive
5. You have two expensive components, CompX and CompY. You want to cache CompX but not CompY. Which is the best way to use <keep-alive> to achieve this?
hard
A.
B.
C.
D.

Solution

  1. Step 1: Identify caching goal

    You want to cache only CompX, not CompY.
  2. Step 2: Use include to cache specific components

    Setting include="CompX" caches only CompX. CompY won't be cached.
  3. Step 3: Check other options

    Exclude CompX caches everything except CompX (wrong). Include CompY caches only CompY (wrong). No filters caches all (wrong).
  4. Final Answer:

    <keep-alive include="CompX"><component :is="currentComp" /></keep-alive> -> Option D
  5. Quick Check:

    Include caches only listed components [OK]
Hint: Use include to cache only desired components [OK]
Common Mistakes:
  • Using exclude to cache only one component
  • Including the wrong component name
  • Not using include or exclude and caching all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard