网页
综合
更新时间 2026-11-14 02:19:04
KeepAlive | Vue.js
自动蜘蛛
vuejs.org
正文约 3217 字
Creating an Application
Reactivity Fundamentals
Class and Style Bindings
Conditional Rendering
Fallthrough Attributes
Single-File Components
Server-Side Rendering (SSR)
Production Deployment
TS with Composition API
Render Functions & JSX
Vue and Web Components
Animation Techniques
<KeepAlive> is a built-in component that allows us to conditionally cache component instances when dynamically switching between multiple components.
In the Component Basics chapter, we introduced the syntax for Dynamic Components, using the <component> special element:
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, we have two stateful components - A contains a counter, while 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
You'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, we'd really like the two component instances to be preserved even when they are inactive. To solve this problem, we can wrap our dynamic component with the <KeepAlive> built-in component:
Now, the state will be persisted across component switches:
Current component: A
Try it in the Playground
Try it in the Playground
When used in in-DOM templates, it should be referenced as <keep-alive>.
By default, <KeepAlive> will cache any component instance inside. We 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:
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.
Since version 3.2.34, a single-file component using <script setup> will automatically infer its name option based on the filename, removing the need to manually declare the name.
We 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.
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 onActivated() and onDeactivated():
A kept-alive component can register lifecycle hooks for these two states using activated and deactivated hooks:
onActivatedactivated is also called on mount, and onDeactivateddeactivated on unmount.
Both hooks work for not only the root component cached by <KeepAlive>, but also the descendant components in the cached tree.
Edit this page on GitHub
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。