Open code, practice, and notes →
Download source with English comments · for reading
Vue · 21 KeepAlive and Pausing
Series contents · Technology map
Stage: Composables and Store Lifetimes · Editable example · Storage ID: vue-step-21
Goals and learning outcomes
- The Editor's local string remains on return.
- The inactive Preview's tick does not increase.
Prerequisites
Unit 21 builds on the work and experiment records from Unit 20.
Key concepts and explanations
Preserving instances
KeepAlive caches inactive component instances. Unlike removal, it can retain local state.
Active lifetime
Cached components can hold resources even while inactive. Design pausing and resuming through onActivated and onDeactivated.
Practice setup
First check the execution scope guidance on the example screen. The following is the preparation environment for extending the assignment in a separate project. Use .vue SFCs and script setup in a separate Vue Vite project. Record package versions and the lockfile, and run on the development server. The built-in Vue examples use the h function and are not a general-purpose SFC compiler.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- Wrap Editor and Preview dynamic components with KeepAlive in src/Workspace.vue.
- Start the Preview timer in onActivated and stop it in onDeactivated.
- Alternate panels 5 times and check local values and active timer counts.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Preservation method | Ordinary dynamic component | KeepAlive | Local values remain and inactive timers must stop. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- The Editor's local string remains on return.
- The inactive Preview's tick does not increase.
Self-check
- Does onUnmounted alone stop a cached screen's loop?
- What should you check when limiting the cache count?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
This is an experiment with actual Vue reactivity and render functions. Check SFC templates in an external compilation environment.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: Async Components and Loading UI · Next: Creating and Releasing Canvas Resources