Skip to content

Commit c233b25

Browse files
feat: added callback function to refs
1 parent 5e33c5b commit c233b25

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/api/special-attributes.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
## ref
3535

36-
- **Expects:** `string`
36+
- **Expects:** `string | Function`
3737

3838
`ref` is used to register a reference to an element or a child component. The reference will be registered under the parent component's `$refs` object. If used on a plain DOM element, the reference will be that element; if used on a child component, the reference will be component instance:
3939

@@ -43,28 +43,31 @@
4343

4444
<!-- vm.$refs.child will be the child component instance -->
4545
<child-component ref="child"></child-component>
46+
47+
<!-- When bound dynamically, we can define ref as a callback function, passing the element or component instance explicitly -->
48+
<child-component :ref="(el) => child = el"></child-component>
4649
```
4750

48-
When used on elements/components with `v-for`, the registered reference will be an Array containing DOM nodes or component instances.
51+
When used on elements/components with `v-for`, the registered reference will be an Array containing DOM nodes or component instances.
4952

50-
An important note about the ref registration timing: because the refs themselves are created as a result of the render function, you cannot access them on the initial render - they don't exist yet! `$refs` is also non-reactive, therefore you should not attempt to use it in templates for data-binding.
53+
An important note about the ref registration timing: because the refs themselves are created as a result of the render function, you cannot access them on the initial render - they don't exist yet! `$refs` is also non-reactive, therefore you should not attempt to use it in templates for data-binding.
5154

5255
- **See also:** [Child Component Refs](../guide/component-template-refs.html)
5356

5457
## is
5558

5659
- **Expects:** `string | Object (component’s options object)`
5760

58-
Used for [dynamic components](../guide/component-dynamic-async.html).
61+
Used for [dynamic components](../guide/component-dynamic-async.html).
5962

60-
For example:
63+
For example:
6164

62-
```html
63-
<!-- component changes when currentView changes -->
64-
<component :is="currentView"></component>
65+
```html
66+
<!-- component changes when currentView changes -->
67+
<component :is="currentView"></component>
6568

66-
For detailed usage, follow the links in the description above.
67-
```
69+
For detailed usage, follow the links in the description above.
70+
```
6871

6972
- **See also:**
7073
- [Dynamic Components](../guide/component-dynamic-async.html)

0 commit comments

Comments
 (0)