Skip to content

Commit 93e0e94

Browse files
committed
keepalive and wip badges
1 parent 76410d5 commit 93e0e94

File tree

11 files changed

+107
-40
lines changed

11 files changed

+107
-40
lines changed

src/guide/best-practices/performance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
aside: deep
33
---
44

5-
# Performance Guide
5+
# Performance Guide <Badge text="WIP" />
66

77
## Page Load Performance
88

src/guide/best-practices/production-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Production Deployment
1+
# Production Deployment <Badge text="WIP" />
22

33
## Development vs. Production
44

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup>
2+
let count = $ref(0)
3+
</script>
4+
5+
<template>
6+
<p>Current component: A</p>
7+
<span style="margin-right:20px">count: {{ count }}</span>
8+
<button @click="count++">+</button>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup>
2+
let msg = $ref('')
3+
</script>
4+
5+
<template>
6+
<p>Current component: B</p>
7+
<span style="margin-right:20px;">Message is: {{ msg }}</span>
8+
<input v-model="msg">
9+
</template>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script setup>
2+
import CompA from './CompA.vue'
3+
import CompB from './CompB.vue'
4+
5+
let current = $shallowRef(CompA)
6+
7+
const { useKeepAlive } = defineProps(['useKeepAlive'])
8+
</script>
9+
10+
<template>
11+
<div class="demo">
12+
<label><input type="radio" v-model="current" :value="CompA" /> A</label>
13+
<label><input type="radio" v-model="current" :value="CompB" /> B</label>
14+
<template v-if="useKeepAlive">
15+
<component :is="current"></component>
16+
</template>
17+
<template v-else>
18+
<KeepAlive>
19+
<component :is="current"></component>
20+
</KeepAlive>
21+
</template>
22+
</div>
23+
</template>

src/guide/built-ins/keep-alive.md

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,82 @@
1+
<script setup>
2+
import SwitchComponent from './keep-alive-demos/SwitchComponent.vue'
3+
</script>
4+
15
# KeepAlive
26

37
## Basic Usage
48

5-
:::tip
6-
When used in [DOM templates](/guide/essentials/component-basics.html#dom-template-parsing-caveats), it should be referenced as `<keep-alive>`.
7-
:::
8-
9-
Earlier, we used the `is` attribute to switch between components in a tabbed interface:
9+
In the Component Basics chapter, we introduced the syntax for [Dynamic Components](/guide/essentials/component-basics.html#dynamic-components), using the `<component>` special element:
1010

1111
```vue-html
12-
<component :is="currentTabComponent"></component>
12+
<component :is="activeComponent" />
1313
```
1414

15-
When switching between these components though, you'll sometimes want to maintain their state or avoid re-rendering for performance reasons. For example, when expanding our tabbed interface a little:
15+
By default, an active component instance will be unmounted when switched away from. This will cause any changed state it holds to be lost.
16+
17+
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 update the state of one of them, switch away, and then switch back to it:
1618

17-
<!-- <common-codepen-snippet title="Dynamic components: without keep-alive" slug="jOPjZOe" tab="html,result" /> -->
19+
<SwitchComponent />
1820

19-
You'll notice that if you select a post, switch to the _Archive_ tab, then switch back to _Posts_, it's no longer showing the post you selected. That's because each time you switch to a new tab, Vue creates a new instance of the `currentTabComponent`.
21+
You'll notice that when switched back, the previous changed state would have been reset.
2022

21-
Recreating dynamic components is normally useful behavior, but in this case, we'd really like those tab component instances to be cached once they're created for the first time. To solve this problem, we can wrap our dynamic component with a `<keep-alive>` element:
23+
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:
2224

2325
```vue-html
2426
<!-- Inactive components will be cached! -->
25-
<keep-alive>
26-
<component :is="currentTabComponent"></component>
27-
</keep-alive>
27+
<KeepAlive>
28+
<component :is="activeComponent" />
29+
</KeepAlive>
2830
```
2931

30-
Check out the result below:
32+
Now, the state will be persisted across component switches:
3133

32-
<!-- <common-codepen-snippet title="Dynamic components: with keep-alive" slug="VwLJQvP" tab="html,result" /> -->
34+
<SwitchComponent use-KeepAlive />
3335

34-
Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered.
36+
<div class="composition-api">
3537

36-
Check out more details on `<keep-alive>` in the [API reference](/api/built-in-components.html#keep-alive).
38+
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHNoYWxsb3dSZWYgfSBmcm9tICd2dWUnXG5pbXBvcnQgQ29tcEEgZnJvbSAnLi9Db21wQS52dWUnXG5pbXBvcnQgQ29tcEIgZnJvbSAnLi9Db21wQi52dWUnXG5cbmNvbnN0IGN1cnJlbnQgPSBzaGFsbG93UmVmKENvbXBBKVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPGRpdiBjbGFzcz1cImRlbW9cIj5cbiAgICA8bGFiZWw+PGlucHV0IHR5cGU9XCJyYWRpb1wiIHYtbW9kZWw9XCJjdXJyZW50XCIgOnZhbHVlPVwiQ29tcEFcIiAvPiBBPC9sYWJlbD5cbiAgICA8bGFiZWw+PGlucHV0IHR5cGU9XCJyYWRpb1wiIHYtbW9kZWw9XCJjdXJyZW50XCIgOnZhbHVlPVwiQ29tcEJcIiAvPiBCPC9sYWJlbD5cbiAgICA8S2VlcEFsaXZlPlxuICAgICAgPGNvbXBvbmVudCA6aXM9XCJjdXJyZW50XCI+PC9jb21wb25lbnQ+XG4gICAgPC9LZWVwQWxpdmU+XG4gIDwvZGl2PlxuPC90ZW1wbGF0ZT5cbiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0iLCJDb21wQS52dWUiOiI8c2NyaXB0I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:::tip
48+
When used in [DOM templates](/guide/essentials/component-basics.html#dom-template-parsing-caveats), it should be referenced as `<keep-alive>`.
49+
:::
3750

3851
## Include / Exclude
3952

53+
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:
54+
55+
```vue-html
56+
<!-- comma-delimited string -->
57+
<KeepAlive include="a,b">
58+
<component :is="view"></component>
59+
</KeepAlive>
60+
61+
<!-- regex (use `v-bind`) -->
62+
<KeepAlive :include="/a|b/">
63+
<component :is="view"></component>
64+
</KeepAlive>
65+
66+
<!-- Array (use `v-bind`) -->
67+
<KeepAlive :include="['a', 'b']">
68+
<component :is="view"></component>
69+
</KeepAlive>
70+
```
71+
72+
The match is checked against the component's [`name`](/api/options-misc.html#name) option, so components that need to be conditionally cached by `KeepAlive` must explicitly delcare a `name` option.
73+
4074
## Max Cached Instances
75+
76+
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](<https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)>): 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.
77+
78+
```vue-html
79+
<KeepAlive :max="10">
80+
<component :is="activeComponent" />
81+
</KeepAlive>
82+
```

src/guide/built-ins/suspense.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Suspense
1+
# Suspense <Badge type="warning" text="experimental" />
22

33
## Basic Usage
44

src/guide/extras/reactivity-in-depth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
aside: deep
33
---
44

5-
# Reactivity in Depth
5+
# Reactivity in Depth <Badge text="WIP" />
66

77
// TODO explain proxies
88
// TODO explain refs

src/guide/extras/render-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Render Functions
1+
# Render Functions <Badge text="WIP" />
22

33
Vue recommends using templates to build applications in the vast majority of cases. However, there are situations where we need the full programmatic power of JavaScript. That's where we can use the **render function**.
44

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
# Rendering Mechanism
2-
3-
> This page is not required reading in order to learn how to use Vue well, but it provides more information, should you be curious how rendering works under the hood.
4-
5-
## Virtual DOM
6-
7-
Now that we know how watchers are updating the components, you might ask how those changes eventually make it to the DOM! Perhaps you’ve heard of the Virtual DOM before, many frameworks including Vue use this paradigm to make sure our interfaces reflect the changes we’re updating in JavaScript effectively
8-
9-
<div class="reactivecontent">
10-
<!-- <common-codepen-snippet title="How does the Virtual DOM work?" slug="KKNJKbw" tab="result" theme="light" :height="500" :editable="false" :preview="false" /> -->
11-
</div>
12-
13-
We make a copy of the DOM in JavaScript called the Virtual DOM, we do this because touching the DOM with JavaScript is computationally expensive. While performing updates in JavaScript is cheap, finding the required DOM nodes and updating them with JavaScript is expensive. So we batch calls, and change the DOM all at once.
14-
15-
The Virtual DOM is a lightweight JavaScript object, created by a render function. It takes three arguments: the element, an object with data, props, attrs and more, and an array. The array is where we pass in the children, which have all these arguments too, and then they can have children and so on, until we build a full tree of elements.
16-
17-
If we need to update the list items, we do so in JavaScript, using the reactivity we mentioned earlier. We then make all the changes to the JavaScript copy, the virtual DOM, and perform a diff between this and the actual DOM. Only then do we make our updates to just what has changed. The Virtual DOM allows us to make performant updates to our UIs!
1+
# Rendering Mechanism <Badge text="WIP" />

0 commit comments

Comments
 (0)