You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/composition-api-template-refs.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
# Template Refs
1
+
# _refs_ de Template
2
2
3
-
> This section uses [single-file component](single-file-component.html)syntax for code examples
3
+
> Esta seção usa a sintaxe de [componente single-file](single-file-component.html)para exemplos de código
4
4
5
-
> This guide assumes that you have already read the [Composition API Introduction](composition-api-introduction.html)and [Reactivity Fundamentals](reactivity-fundamentals.html). Read that first if you are new to Composition API.
5
+
> Este guia pressupõe que você já leu a [Introdução à API de Composição](composition-api-introduction.html)e os [Fundamentos da Reatividade](reactivity-fundamentals.html). Leia lá primeiro se você for novo na API de Composição.
6
6
7
-
When using the Composition API, the concept of [reactive refs](reactivity-fundamentals.html#creating-standalone-reactive-values-as-refs)and [template refs](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()](composition-api-setup.html):
7
+
Ao usar a API de Composição, os conceitos de [_refs_ reativos](reactivity-fundamentals.html#criacao-de-valores-reativos-avulsos-como-refs)e [_refs_ de _template_](component-template-refs.html)são unificados. Para obter uma referência a um elemento do _template_ ou instância de componente, podemos declarar uma referência como de costume e retorná-la de[setup()](composition-api-setup.html):
8
8
9
9
```html
10
10
<template>
11
-
<divref="root">This is a root element</div>
11
+
<divref="root">Este é um elemento raiz</div>
12
12
</template>
13
13
14
14
<script>
@@ -19,8 +19,8 @@ When using the Composition API, the concept of [reactive refs](reactivity-fundam
19
19
constroot=ref(null)
20
20
21
21
onMounted(() => {
22
-
//the DOM element will be assigned to the ref after initial render
23
-
console.log(root.value) // <div>This is a root element</div>
22
+
//o elemento DOM será atribuído ao ref após a renderização inicial
23
+
console.log(root.value) // <div>Este é um elemento raiz</div>
24
24
})
25
25
26
26
return {
@@ -31,11 +31,11 @@ When using the Composition API, the concept of [reactive refs](reactivity-fundam
31
31
</script>
32
32
```
33
33
34
-
Here we are exposing `root`on the render context and binding it to the div as its ref via `ref="root"`. In the Virtual DOM patching algorithm, if a VNode's`ref`key corresponds to a ref on the render context, the VNode's corresponding element or component instance will be assigned to the value of that ref. This is performed during the Virtual DOM mount / patch process, so template refs will only get assigned values after the initial render.
34
+
Aqui estamos expondo `root`no contexto de renderização e vinculando-o a div como sua referência via `ref="root"`. No algoritmo de correção (_patch_) do DOM Virtual, se a chave`ref`de um VNode corresponder a uma referência no contexto de renderização, o elemento ou instância de componente correspondente do VNode será atribuído ao valor dessa referência. Isso é realizado durante o processo de montagem / _patch_ do Virtual DOM, portanto, as referências do _template_ só receberão valores atribuídos após a renderização inicial.
35
35
36
-
Refs used as templates refs behave just like any other refs: they are reactive and can be passed into (or returned from) composition functions.
36
+
Referências usadas como _refs_ de _template_ se comportam como quaisquer outras _refs_: elas são reativas e podem ser passadas (ou retornadas) a funções de composição.
37
37
38
-
## Usage with JSX
38
+
## Uso com JSX
39
39
40
40
```js
41
41
exportdefault {
@@ -47,15 +47,15 @@ export default {
47
47
ref: root
48
48
})
49
49
50
-
//with JSX
50
+
//com JSX
51
51
return () =><div ref={root} />
52
52
}
53
53
}
54
54
```
55
55
56
-
## Usage inside`v-for`
56
+
## Uso dentro de`v-for`
57
57
58
-
Composition API template refs do not have special handling when used inside`v-for`. Instead, use function refs to perform custom handling:
58
+
As _refs_ de _template_ da API de Composição não têm tratamento especial quando usadas dentro de`v-for`. Em vez disso, use a sintaxe de função nas _refs_ para realizar um tratamento personalizado:
59
59
60
60
```html
61
61
<template>
@@ -72,7 +72,7 @@ Composition API template refs do not have special handling when used inside `v-f
72
72
constlist=reactive([1, 2, 3])
73
73
constdivs=ref([])
74
74
75
-
//make sure to reset the refs before each update
75
+
//certifique-se de limpar as refs antes de cada atualização
76
76
onBeforeUpdate(() => {
77
77
divs.value= []
78
78
})
@@ -86,15 +86,15 @@ Composition API template refs do not have special handling when used inside `v-f
86
86
</script>
87
87
```
88
88
89
-
## Watching Template Refs
89
+
## Observando _refs_ de Templates
90
90
91
-
Watching a template ref for changes can be an alternative to the use of lifecycle hooks that was demonstrated in the previous examples.
91
+
Observar uma referência de _template_ por alterações pode ser uma alternativa ao uso de gatilhos de ciclo de vida que foi demonstrado nos exemplos anteriores.
92
92
93
-
But a key difference to lifecycle hooks is that `watch()`and`watchEffect()`effects are run *before* the DOM is mounted or updated so the template ref hasn't been updated when the watcher runs the effect:
93
+
Mas uma diferença chave para os gatilhos do ciclo de vida é que os efeitos `watch()`e`watchEffect()`são executados *antes* do DOM ser montado ou atualizado, de modo que o _ref_ de _template_ ainda não foi atualizado quando o observador executa o efeito:
94
94
95
95
```vue
96
96
<template>
97
-
<div ref="root">This is a root element</div>
97
+
<div ref="root">Este é um elemento raiz</div>
98
98
</template>
99
99
100
100
<script>
@@ -105,8 +105,8 @@ But a key difference to lifecycle hooks is that `watch()` and `watchEffect()` ef
105
105
const root = ref(null)
106
106
107
107
watchEffect(() => {
108
-
// This effect runs before the DOM is updated, and consequently,
109
-
// the template ref does not hold a reference to the element yet.
108
+
// Este efeito é executado antes que o DOM seja atualizado e, consequentemente,
109
+
// o ref de template ainda não contém uma referência ao elemento.
110
110
console.log(root.value) // => null
111
111
})
112
112
@@ -118,11 +118,11 @@ But a key difference to lifecycle hooks is that `watch()` and `watchEffect()` ef
118
118
</script>
119
119
```
120
120
121
-
Therefore, watchers that use template refs should be defined with the `flush: 'post'` option. This will run the effect *after* the DOM has been updated and ensure that the template ref stays in sync with the DOM and references the correct element.
121
+
Portanto, observadores que usam _refs_ de _template_ devem ser definidos com a opção `flush: 'post'`. Isso executará o efeito *depois* do DOM ser atualizado e garantirá que o _ref_ de _template_ permaneça em sincronia com o DOM e faça referência ao elemento correto.
122
122
123
123
```vue
124
124
<template>
125
-
<div ref="root">This is a root element</div>
125
+
<div ref="root">Este é um elemento raiz</div>
126
126
</template>
127
127
128
128
<script>
@@ -133,7 +133,7 @@ Therefore, watchers that use template refs should be defined with the `flush: 'p
133
133
const root = ref(null)
134
134
135
135
watchEffect(() => {
136
-
console.log(root.value) // => <div>This is a root element</div>
136
+
console.log(root.value) // => <div>Este é um elemento raiz</div>
137
137
},
138
138
{
139
139
flush: 'post'
@@ -147,4 +147,4 @@ Therefore, watchers that use template refs should be defined with the `flush: 'p
147
147
</script>
148
148
```
149
149
150
-
*See also: [Computed and Watchers](./reactivity-computed-watchers.html#effect-flush-timing)
150
+
*Ver também: [Dados Computados e Observadores](./reactivity-computed-watchers.html#effect-flush-timing)
0 commit comments