Keep Alive
<KeepAlive>
is a built-in component that allows us to conditionally cache component instances when dynamically switching between multiple components.
Basic Usage
Section titled “Basic Usage”In the Component Basics chapter, you learnt about the syntax for Dynamic Components, using the <component>
special element:
<component :is="activeComponent" />
By default, an active component instance will be unmounted when switching away from it. This will cause any changed state it holds to be lost. When this component is displayed again, a new instance will be created with only the initial state.
In the example below, there are two stateful components:
- A contains a counter
- B contains a message synced with an input via
v-model
.
Try updating the state of one of them, switch away, and then switch back to it:
Current component: A
count: 0You’ll notice that when switched back, the previous changed state would have been reset.
Creating fresh component instance on switch is normally useful behavior, but in this case, you’d really like the two component instances to be preserved even when they are inactive. To solve this problem, you can wrap your dynamic component with the <KeepAlive>
built-in component. This way the state will be persisted across component switches:
<script>import CompA from './CompA.vue'import CompB from './CompB.vue'
export default {components: { CompA, CompB },data() { return { current: 'CompA' }}}</script>
<template><div class="demo"> <label><input type="radio" v-model="current" value="CompA" /> A</label> <label><input type="radio" v-model="current" value="CompB" /> B</label> <KeepAlive> <component :is="current"></component> </KeepAlive></div></template>
<script>export default {data() { return { count: 0 }}}</script>
<template><p>Current component: A</p><span>count: {{ count }}</span><button @click="count++">+</button></template>
<script>export default {data() { return { msg: '' }}}</script>
<template><p>Current component: B</p><span>Message is: {{ msg }}</span><input v-model="msg"></template>
Current component: A
count: 0Try it in playground
Include / Exclude
Section titled “Include / Exclude”By default, <KeepAlive>
will cache any component instance inside. You can customize this behavior via the include
and exclude
props. Both props can be a comma-delimited string, a RegExp
, or an array containing either types:
<!-- comma-delimited string --><KeepAlive include="a,b"> <component :is="view" /></KeepAlive>
<!-- regex (use `v-bind`) --><KeepAlive :include="/a|b/"> <component :is="view" /></KeepAlive>
<!-- Array (use `v-bind`) --><KeepAlive :include="['a', 'b']"> <component :is="view" /></KeepAlive>
The match is checked against the component’s name
option, so components that need to be conditionally cached by KeepAlive
must explicitly declare a name
option.
Max Cached Instances
Section titled “Max Cached Instances”You can limit the maximum number of component instances that can be cached via the max
prop. When max
is specified, <KeepAlive>
behaves like an LRU cache: if the number of cached instances is about to exceed the specified max count, the least recently accessed cached instance will be destroyed to make room for the new one.
<KeepAlive :max="10"><component :is="activeComponent" /></KeepAlive>
Lifecycle of Cached Instance
When a component instance is removed from the DOM but is part of a component tree cached by <KeepAlive>
, it goes into a deactivated state instead of being unmounted. When a component instance is inserted into the DOM as part of a cached tree, it is activated.
A kept-alive component can register lifecycle hooks for these two states using activated
and deactivated
hooks:
export default {activated() { // called on initial mount // and every time it is re-inserted from the cache},deactivated() { // called when removed from the DOM into the cache // and also when unmounted}}
Note that:
-
activated
is also called on mount, anddeactivated
on unmount. -
Both hooks work for not only the root component cached by
<KeepAlive>
, but also the descendant components in the cached tree.
Related