Skip to content

Commit e92d8d9

Browse files
alexsashareganchrisvfritz
authored andcommitted
feat(render-function): adds functional template ex for passing attrs (#1454)
* feat(render-function): adds functional template ex for passing attrs * Simplify functional template example
1 parent dc60ee9 commit e92d8d9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/v2/guide/render-function.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ Vue.component('smart-list', {
581581

582582
### Passing Attributes and Events to Child Elements/Components
583583

584-
On normal components, attributes not defined as props are automatically added to the root element of the component, replacing or [intelligently merging with](class-and-style.html) any existing attributes of the same name.
584+
On normal components, attributes not defined as props are automatically added to the root element of the component, replacing or [intelligently merging with](class-and-style.html) any existing attributes of the same name.
585585

586586
Functional components, however, require you to explicitly define this behavior:
587587

@@ -597,6 +597,20 @@ Vue.component('my-functional-button', {
597597

598598
By passing `context.data` as the second argument to `createElement`, we are passing down any attributes or event listeners used on `my-functional-button`. It's so transparent, in fact, that events don't even require the `.native` modifier.
599599

600+
If you are using template-based functional components, you will also have to manually add attributes and listeners. Since we have access to the individual context contents, we can use `data.attrs` to pass along any HTML attributes and `listeners` _(the alias for `data.on`)_ to pass along any event listeners.
601+
602+
```html
603+
<template functional>
604+
<button
605+
class="btn btn-primary"
606+
v-bind="data.attrs"
607+
v-on="listeners"
608+
>
609+
<slot/>
610+
</button>
611+
</template>
612+
```
613+
600614
### `slots()` vs `children`
601615

602616
You may wonder why we need both `slots()` and `children`. Wouldn't `slots().default` be the same as `children`? In some cases, yes - but what if you have a functional component with the following children?

0 commit comments

Comments
 (0)