Skip to content

Commit bdd72ab

Browse files
Template refs (Options API) (#117)
* feat: added template refs * Update src/guide/component-template-refs.md Co-authored-by: Phan An <[email protected]> * fix: refactored template refs Co-authored-by: Phan An <[email protected]>
1 parent 212efc4 commit bdd72ab

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const sidebar = {
2727
'/guide/component-slots',
2828
'/guide/component-provide-inject',
2929
'/guide/component-dynamic-async',
30+
'/guide/component-template-refs',
3031
'/guide/component-edge-cases'
3132
]
3233
},

src/api/composition-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ const foo = inject<string>('foo') // string | undefined
291291

292292
## Template Refs
293293

294-
When using the Composition API, the concept of [reactive refs](./refs-api.html#ref) and [template refs](TODO) are unified. In order to obtain a reference to an in-template element or component instance, we can declare a ref as usual and return it from `setup()`:
294+
When using the Composition API, the concept of [reactive refs](./refs-api.html#ref) and [template refs](../guide/component-template-refs.html) are unified. In order to obtain a reference to an in-template element or component instance, we can declare a ref as usual and return it from `setup()`:
295295

296296
```html
297297
<template>

src/api/instance-properties.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@
126126

127127
- **Details:**
128128

129-
An object of DOM elements and component instances, registered with [`ref` attributes](TODO).
129+
An object of DOM elements and component instances, registered with [`ref` attributes](../guide/component-template-refs.html).
130130

131131
- **See also:**
132-
- [Child Component Refs](TODO:refs)
132+
- [Template refs](../guide/component-template-refs.html)
133133
- [Special Attributes - ref](./special-attributes.md#ref)
134134

135135
## $attrs

src/api/special-attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
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.
5151

52-
- **See also:** [Child Component Refs](TODO-handling-edge-cases)
52+
- **See also:** [Child Component Refs](../guide/component-template-refs.html)
5353

5454
## is
5555

src/guide/component-template-refs.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Template refs
2+
3+
> This page assumes you've already read the [Components Basics](component-basics.md). Read that first if you are new to components.
4+
5+
Despite the existence of props and events, sometimes you might still need to directly access a child component in JavaScript. To achieve this you can assign a reference ID to the child component or HTML element using the `ref` attribute. For example:
6+
7+
```html
8+
<input ref="input" />
9+
```
10+
11+
This may be useful when you want to, for example, programmatically focus this input on component mount:
12+
13+
```js
14+
const app = Vue.createApp({})
15+
16+
app.component('base-input', {
17+
template: `
18+
<input ref="input" />
19+
`,
20+
methods: {
21+
focusInput() {
22+
this.$refs.input.focus()
23+
}
24+
},
25+
mounted() {
26+
this.focusInput()
27+
}
28+
})
29+
```
30+
31+
Also, you can add another `ref` to the component itself and use it to trigger `focusInput` event from the parent component:
32+
33+
```html
34+
<base-input ref="usernameInput"></base-input>
35+
```
36+
37+
```js
38+
this.$refs.usernameInput.focusInput()
39+
```
40+
41+
When `ref` is used together with `v-for`, the ref you get will be an array containing the child components mirroring the data source.
42+
43+
::: warning
44+
`$refs` are only populated after the component has been rendered. It is only meant as an escape hatch for direct child manipulation - you should avoid accessing `$refs` from within templates or computed properties.
45+
:::
46+
47+
**See also**: [Using template refs in Composition API](../api/composition-api.html#template-refs)

0 commit comments

Comments
 (0)