Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

Vue Tutorial

Vue HOMEVue IntroVue DirectivesVue v-bindVue v-ifVue v-showVue v-forVue EventsVue v-onVue MethodsVue Event ModifiersVue FormsVue v-modelVue CSS BindingVue Computed PropertiesVue WatchersVue Templates

Scaling Up

Vue Why, How and SetupVue First SFC PageVue ComponentsVue PropsVue v-for ComponentsVue $emit()Vue Fallthrough AttributesVue Scoped StylingVue Local ComponentsVue SlotsVue v-slotVue Scoped SlotsVue Dynamic ComponentsVue TeleportVue HTTP RequestVue Template RefsVue Lifecycle HooksVue Provide/InjectVue RoutingVue Form InputsVue AnimationsVue Animations with v-forVue BuildVue Composition API

Vue Reference

Vue Built-in AttributesVue Built-in ComponentsVue Built-in ElementsVue Component InstanceVue DirectivesVue Instance OptionsVue Lifecycle Hooks

Vue Examples

Vue ExamplesVue ExercisesVue QuizVue SyllabusVue Study PlanVue ServerVue Certificate

Vue <KeepAlive> Component


Example

Using the built-in<KeepAlive> component to make the components keep the previously entered user inputs:

<KeepAlive>  <component :is="activeComp"></component></KeepAlive>
Run Example »

See more examples below.


Definition and Usage

The built-in<KeepAlive> component is used around dynamic components to cache the components when they are not active, so that their state is kept.


Props

The<KeepAlive> component can be used together with different props so that we can specify which components that should be cached so that they keep their state.

PropDescription
noneAll components are cached so that their state is keptRun Example »
includeOptional. Specify the names of the components that should keep their stateRun Example »
excludeOptional. Specify the names of the components that shouldnot keep their stateRun Example »
maxOptional. Specify the maximum number of components that should keep their state. If you specify to cache a maximum of 4 components, it will be the 4 components that were last visited that are cachedRun Example »

The Lifecycle of Components Cached with <KeepAlive>

Components that are cached with the built-in<KeepAlive> component will switch between theactivated anddeactivated states when they are set, or not set, as the active dynamic component.

Theactivated() anddeactivated() lifecycle hooks can be used to run code when such a cached component is set, or not set, as the active component.


More Examples

Example

Using theinclude prop to specify which components that will cache the values:

<KeepAlive include="CompOne,CompThree">  <component :is="activeComp"></component></KeepAlive>
Run Example »

Example

Using theexclude prop to specify which components that willnot cache the values:

<KeepAlive exclude="CompOne">  <component :is="activeComp"></component></KeepAlive>
Run Example »

Example

Using themax prop to specify how many of the last visited components that will cache the values:

<KeepAlive :max="2">  <component :is="activeComp"></component></KeepAlive>
Run Example »

Related Pages

Vue Tutorial:Dynamic Components

Vue Tutorial:The 'activated' and 'deactivated' Lifecycle Hooks



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp