Skip to content

Commit dbbd5c5

Browse files
authored
Merge pull request #219 from LucasCastro99/patch-4
Tradução de "guide/migration/custom-directives.md"
2 parents 117c26f + ada836b commit dbbd5c5

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
---
22
badges:
3-
- breaking
3+
- Urgente
44
---
55

6-
# Custom Directives <MigrationBadges :badges="$frontmatter.badges" />
6+
# Diretivas Personalizadas <MigrationBadges :badges="$frontmatter.badges" />
77

8-
## Overview
8+
## Visão Geral
99

10-
Here is a quick summary of what has changed:
10+
Aqui está um rápido resumo do que mudou:
1111

12-
- API has been renamed to better align with component lifecycle
13-
- Custom directives will be controlled by child component via `v-bind="$attrs"`
12+
- API foi renomeada para se alinhar melhor com o ciclo de vida do componente
13+
- As diretivas personalizadas serão controladas pelo componente filho via `v-bind="$attrs"`
1414

15-
For more information, read on!
15+
Para mais informações, continue lendo!
1616

17-
## 2.x Syntax
17+
## Sintaxe 2.x
1818

19-
In Vue 2, custom directives were created by using the hooks listed below to target an element’s lifecycle, all of which are optional:
19+
No Vue 2, as diretivas personalizadas foram criadas usando os ganchos listados abaixo para direcionar o ciclo de vida de um elemento, todos opcionais:
2020

21-
- **bind** - Occurs once the directive is bound to the element. Occurs only once.
22-
- **inserted** - Occurs once the element is inserted into the parent DOM.
23-
- **update** - This hook is called when the element updates, but children haven't been updated yet.
24-
- **componentUpdated** - This hook is called once the component and the children have been updated.
25-
- **unbind** - This hook is called once the directive is removed. Also called only once.
21+
- **bind** - Ocorre quando a diretiva é vinculada ao elemento. Ocorre apenas uma vez.
22+
- **inserted** - Ocorre quando o elemento é inserido no DOM pai.
23+
- **update** - Este gancho é chamado quando o elemento é atualizado, mas os filhos ainda não foram atualizados.
24+
- **componentUpdated** - Este gancho é chamado assim que o componente e os filhos forem atualizados.
25+
- **unbind** - Este gancho é chamado assim que a diretiva é removida. Também chamado apenas uma vez.
2626

27-
Here’s an example of this:
27+
Aqui está um exemplo disso:
2828

2929
```html
30-
<p v-highlight="yellow">Highlight this text bright yellow</p>
30+
<p v-highlight="yellow">Destaque este texto em amarelo claro</p>
3131
```
3232

3333
```js
@@ -38,37 +38,37 @@ Vue.directive('highlight', {
3838
})
3939
```
4040

41-
Here, in the initial setup for this element, the directive binds a style by passing in a value, that can be updated to different values through the application.
41+
Aqui, na configuração inicial desse elemento, a diretiva vincula um estilo passando um valor, que pode ser atualizado para diferentes valores por meio da aplicação.
4242

43-
## 3.x Syntax
43+
## Sintaxe 3.x
4444

45-
In Vue 3, however, we’ve created a more cohesive API for custom directives. As you can see, they differ greatly from our component lifecycle methods even though we’re hooking into similar events. We’ve now unified them like so:
45+
No Vue 3, no entanto, criamos uma API mais coesa para diretivas personalizadas. Como você pode ver, eles diferem muito de nossos métodos de ciclo de vida de componentes, embora estejamos nos conectando a eventos semelhantes. Agora, nós os unificamos assim:
4646

4747
- bind → **beforeMount**
4848
- inserted → **mounted**
49-
- **beforeUpdate**: new! this is called before the element itself is updated, much like the component lifecycle hooks.
50-
- update → removed! There were too many similarities to updated, so this is redundant. Please use updated instead.
49+
- **beforeUpdate**: novo! isso é chamado antes que o próprio elemento seja atualizado, de forma muito semelhante aos ganchos do ciclo de vida do componente.
50+
- update → removido! Havia muitas semelhanças para atualizar, então isso é redundante. Em vez disso, use "updated".
5151
- componentUpdated → **updated**
52-
- **beforeUnmount** new! similar to component lifecycle hooks, this will be called right before an element is unmounted.
52+
- **beforeUnmount**: novo! semelhante aos ganchos do ciclo de vida do componente, isso será chamado logo antes de um elemento ser desmontado.
5353
- unbind -> **unmounted**
5454

55-
The final API is as follows:
55+
A API final é a seguinte:
5656

5757
```js
5858
const MyDirective = {
5959
beforeMount(el, binding, vnode, prevVnode) {},
6060
mounted() {},
6161
beforeUpdate() {},
6262
updated() {},
63-
beforeUnmount() {}, // new
63+
beforeUnmount() {}, // novo
6464
unmounted() {}
6565
}
6666
```
6767

68-
The resulting API could be used like this, mirroring the example from earlier:
68+
A API resultante poderia ser usada assim, espelhando o exemplo anterior:
6969

7070
```html
71-
<p v-highlight="yellow">Highlight this text bright yellow</p>
71+
<p v-highlight="yellow">Destaque este texto em amarelo claro</p>
7272
```
7373

7474
```js
@@ -81,48 +81,48 @@ app.directive('highlight', {
8181
})
8282
```
8383

84-
Now that the custom directive lifecycle hooks mirror those of the components themselves, they become easier to reason about and remember!
84+
Agora que os ganchos do ciclo de vida da diretiva personalizada espelham os dos próprios componentes, eles se tornam mais fáceis de raciocinar e lembrar!
8585

86-
### Edge Case: Accessing the component instance
86+
### Caso Isolado: Acessando a instância do componente
8787

88-
It's generally recommended to keep directives independent of the component instance they are used in. Accessing the instance from within a custom directive is often a sign that the directive should rather be a component itself. However, there are situations where this actually makes sense.
88+
Geralmente, é recomendado manter as diretivas independentes da instância do componente em que são usadas. Acessar a instância a partir de uma diretiva personalizada costuma ser um sinal de que a diretiva deve ser um componente em si. No entanto, existem situações em que isso realmente faz sentido.
8989

90-
In Vue 2, the component instance had to be accessed through the `vnode` argument:
90+
No Vue 2, a instância do componente tinha que ser acessada por meio do argumento `vnode`:
9191

9292
```javascript
9393
bind(el, binding, vnode) {
9494
const vm = vnode.context
9595
}
9696
```
9797

98-
In Vue 3, the instance is now part of the `binding`:
98+
No Vue 3, a instância agora faz parte do `binding`:
9999

100100
```javascript
101101
mounted(el, binding, vnode) {
102102
const vm = binding.instance
103103
}
104104
```
105105

106-
## Implementation Details
106+
## Detalhes de Implementação
107107

108-
In Vue 3, we're now supporting fragments, which allow us to return more than one DOM node per component. You can imagine how handy that is for something like a component with multiple `<li>` elements or the children elements of a table:
108+
No Vue 3, agora oferecemos suporte a fragmentos, o que nos permite retornar mais de um nó DOM por componente. Você pode imaginar como isso é útil para algo como um componente com vários elementos `<li>` ou os elementos filhos de uma tabela:
109109

110110
```html
111111
<template>
112-
<li>Hello</li>
113-
<li>Vue</li>
114-
<li>Devs!</li>
112+
<li>Olá</li>
113+
<li>Desenvolvedores</li>
114+
<li>Vue!</li>
115115
</template>
116116
```
117117

118-
As wonderfully flexible as this is, we can potentially encounter a problem with a custom directive that could have multiple root nodes.
118+
Tão maravilhosamente flexível quanto isso é, podemos potencialmente encontrar um problema com uma diretiva personalizada que pode ter vários nós raiz.
119119

120-
As a result, custom directives are now included as part of a virtual DOM node’s data. When a custom directive is used on a component, hooks are passed down to the component as extraneous props and end up in `this.$attrs`.
120+
Como resultado, as diretivas personalizadas agora são incluídas como parte dos dados de um nó virtual do DOM. Quando uma diretiva personalizada é usada em um componente, ganchos são passados para o componente como props estranhos e acabam em `this.$attrs`.
121121

122-
This also means it's possible to directly hook into an element's lifecycle like this in the template, which can be handy when a custom directive is too involved:
122+
Isso também significa que é possível conectar-se diretamente ao ciclo de vida de um elemento como este modelo, o que pode ser útil quando uma diretiva personalizada está muito envolvida:
123123

124124
```html
125125
<div @vnodeMounted="myHook" />
126126
```
127127

128-
This is consistent with the attribute fallthrough behavior, so when a child component uses `v-bind="$attrs"` on an inner element, it will apply any custom directives used on it as well.
128+
Isso é consistente com o comportamento de "fallthrough" do atributo, portanto, quando um componente filho usa `v-bind="$attrs"` em um elemento interno, ele também aplica todas as diretivas personalizadas usadas nele.

0 commit comments

Comments
 (0)