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
- 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"`
14
14
15
-
For more information, read on!
15
+
Para mais informações, continue lendo!
16
16
17
-
## 2.x Syntax
17
+
## Sintaxe 2.x
18
18
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:
20
20
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.
26
26
27
-
Here’s an example of this:
27
+
Aqui está um exemplo disso:
28
28
29
29
```html
30
-
<pv-highlight="yellow">Highlight this text bright yellow</p>
30
+
<pv-highlight="yellow">Destaque este texto em amarelo claro</p>
31
31
```
32
32
33
33
```js
@@ -38,37 +38,37 @@ Vue.directive('highlight', {
38
38
})
39
39
```
40
40
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.
42
42
43
-
## 3.x Syntax
43
+
## Sintaxe 3.x
44
44
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:
46
46
47
47
- bind → **beforeMount**
48
48
- 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".
51
51
- 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.
53
53
- unbind -> **unmounted**
54
54
55
-
The final API is as follows:
55
+
A API final é a seguinte:
56
56
57
57
```js
58
58
constMyDirective= {
59
59
beforeMount(el, binding, vnode, prevVnode) {},
60
60
mounted() {},
61
61
beforeUpdate() {},
62
62
updated() {},
63
-
beforeUnmount() {}, //new
63
+
beforeUnmount() {}, //novo
64
64
unmounted() {}
65
65
}
66
66
```
67
67
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:
69
69
70
70
```html
71
-
<pv-highlight="yellow">Highlight this text bright yellow</p>
71
+
<pv-highlight="yellow">Destaque este texto em amarelo claro</p>
72
72
```
73
73
74
74
```js
@@ -81,48 +81,48 @@ app.directive('highlight', {
81
81
})
82
82
```
83
83
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!
85
85
86
-
### Edge Case: Accessing the component instance
86
+
### Caso Isolado: Acessando a instância do componente
87
87
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.
89
89
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`:
91
91
92
92
```javascript
93
93
bind(el, binding, vnode) {
94
94
constvm=vnode.context
95
95
}
96
96
```
97
97
98
-
In Vue 3, the instance is now part of the`binding`:
98
+
No Vue 3, a instância agora faz parte do`binding`:
99
99
100
100
```javascript
101
101
mounted(el, binding, vnode) {
102
102
constvm=binding.instance
103
103
}
104
104
```
105
105
106
-
## Implementation Details
106
+
## Detalhes de Implementação
107
107
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:
109
109
110
110
```html
111
111
<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>
115
115
</template>
116
116
```
117
117
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.
119
119
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`.
121
121
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:
123
123
124
124
```html
125
125
<div@vnodeMounted="myHook" />
126
126
```
127
127
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