<KeepAlive> cache component instance khi unmount — state được giữ nguyên khi switch lại:
vue
<KeepAlive :include="['FormStep1', 'FormStep2']" :max="5">
<component :is="currentTab" />
</KeepAlive>- Lifecycle hooks cho cached components:
onActivated(khi cache hit, component show lại),onDeactivated(khi bị hide, không unmount). - Dùng cho: tab views, multi-step forms, expensive components cần giữ state.
- Lưu ý: cached components vẫn chiếm bộ nhớ — dùng
:maxđể limit, tránh cache tất cả.
<KeepAlive> caches component instances when unmounted — state is preserved when switching back:
vue
<KeepAlive :include="['FormStep1', 'FormStep2']" :max="5">
<component :is="currentTab" />
</KeepAlive>- Lifecycle hooks for cached components:
onActivated(on cache hit, component shown again),onDeactivated(when hidden, not unmounted). - Use for: tab views, multi-step forms, expensive components needing state preservation.
Pitfall: cached components still consume memory — use :max to limit, avoid caching everything.