Skip to content

Commit 588c1f6

Browse files
Using built-in components with render and :is (#946)
* docs: explain how to use built-in components with both render and :is * Update src/api/built-in-components.md Co-authored-by: Ben Hong <[email protected]> * Update src/api/built-in-components.md Co-authored-by: Ben Hong <[email protected]> * Update src/guide/render-function.md Co-authored-by: Ben Hong <[email protected]> Co-authored-by: Ben Hong <[email protected]>
1 parent e95dc4c commit 588c1f6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/api/built-in-components.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Built-In Components
22

3+
Built-in components can be used directly in templates without needing to be registered.
4+
5+
The `<keep-alive>`, `<transition>`, `<transition-group>`, and `<teleport>` components can all be tree-shaken by bundlers, so that they are only included in the build if they're used. They can also be imported explicitly if you need direct access to the component itself:
6+
7+
```js
8+
// CDN build of Vue
9+
const { KeepAlive, Teleport, Transition, TransitionGroup } = Vue
10+
```
11+
12+
```js
13+
// ESM build of Vue
14+
import { KeepAlive, Teleport, Transition, TransitionGroup } from 'vue'
15+
```
16+
17+
`<component>` and `<slot>` are component-like features of template syntax. They are not true components and they can't be imported like the components shown above.
18+
319
## component
420

521
- **Props:**
@@ -10,8 +26,6 @@
1026

1127
A "meta component" for rendering dynamic components. The actual component to render is determined by the `is` prop. An `is` prop as a string could be either an HTML tag name or a Component name.
1228

13-
- **Example:**
14-
1529
```html
1630
<!-- a dynamic component controlled by -->
1731
<!-- the `componentId` property on the vm -->
@@ -27,6 +41,27 @@
2741
<component :is="href ? 'a' : 'span'"></component>
2842
```
2943

44+
The built-in components `KeepAlive`, `Transition`, `TransitionGroup`, and `Teleport` can all be passed to `is`, but you must register them if you want to pass them by name. For example:
45+
46+
```js
47+
const { Transition, TransitionGroup } = Vue
48+
49+
const Component = {
50+
components: {
51+
Transition,
52+
TransitionGroup
53+
},
54+
55+
template: `
56+
<component :is="isGroup ? 'TransitionGroup' : 'Transition'">
57+
...
58+
</component>
59+
`
60+
}
61+
```
62+
63+
Registration is not required if you pass the component itself to `is` rather than its name.
64+
3065
- **See also:** [Dynamic Components](../guide/component-dynamic-async.html)
3166

3267
## transition

src/guide/render-function.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,22 @@ render () {
551551

552552
[`resolveDirective`](/api/global-api.html#resolvedirective) is the same function that templates use internally to resolve directives by name. That is only necessary if you don't already have direct access to the directive's definition object.
553553

554+
### Built-in Components
555+
556+
[Built-in components](/api/built-in-components.html) such as `<keep-alive>`, `<transition>`, `<transition-group>`, and `<teleport>` are not registered globally by default. This allows bundlers to perform tree-shaking, so that the components are only included in the build if they are used. However, that also means we can't access them using `resolveComponent` or `resolveDynamicComponent`.
557+
558+
Templates have special handling for those components, automatically importing them when they are used. When we're writing our own `render` functions, we need to import them ourselves:
559+
560+
```js
561+
const { h, KeepAlive, Teleport, Transition, TransitionGroup } = Vue
562+
563+
// ...
564+
565+
render () {
566+
return h(Transition, { mode: 'out-in' }, /* ... */)
567+
}
568+
```
569+
554570
## JSX
555571

556572
If we're writing a lot of `render` functions, it might feel painful to write something like this:

0 commit comments

Comments
 (0)