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-provide-inject.md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
# Provide / Inject
1
+
# Prover e Injetar Dados
2
2
3
-
> This guide assumes that you have already read [Provide / Inject](component-provide-inject.html), [Composition API Introduction](composition-api-introduction.html), and [Reactivity Fundamentals](reactivity-fundamentals.html).
3
+
> Este guia assume que você já leu as seções [Prover e Injetar Dados](component-provide-inject.html), [Introdução a API de Composição](composition-api-introduction.html) e [Fundamentos de Reatividade](reactivity-fundamentals.html).
4
4
5
-
We can use [provide / inject](component-provide-inject.html)with the Composition API as well. Both can only be called during[`setup()`](composition-api-setup.html)with a current active instance.
5
+
Podemos [prover e injetar dados](component-provide-inject.html)com a API de Composição também. Ambos só podem ser chamados durante[`setup()`](composition-api-setup.html)com uma instância ativa atual.
6
6
7
-
## Scenario Background
7
+
## Exemplo de Cenário
8
8
9
-
Let's assume that we want to rewrite the following code, which contains a `MyMap`component that provides a`MyMarker`component with the user's location, using the Composition API.
9
+
Vamos supor que queremos reescrever o código a seguir, que contém um componente `MyMap`que provê um componente`MyMarker`com a localização do usuário, usando a API de Composição.
10
10
11
11
```vue
12
12
<!-- src/components/MyMap.vue -->
@@ -22,7 +22,7 @@ export default {
22
22
MyMarker
23
23
},
24
24
provide: {
25
-
location: 'North Pole',
25
+
location: 'Polo Norte',
26
26
geolocation: {
27
27
longitude: 90,
28
28
latitude: 135
@@ -41,16 +41,16 @@ export default {
41
41
</script>
42
42
```
43
43
44
-
## Using Provide
44
+
## Usando Provide
45
45
46
-
When using`provide`in`setup()`, we start by explicitly importing the method from `vue`. This allows us to define each property with its own invocation of`provide`.
46
+
Ao usar`provide`no`setup()`, começamos importando explicitamente o método de `vue`. Isso nos permite definir cada propriedade com sua própria invocação de`provide`.
47
47
48
-
The `provide`function allows you to define the property through two parameters:
48
+
A função `provide`permite definir a propriedade por meio de dois parâmetros:
49
49
50
-
1.The property's name (`<String>` type)
51
-
2.The property's value
50
+
1.O nome da propriedade (do tipo `<String>`); e
51
+
2.O valor da propriedade.
52
52
53
-
Using our `MyMap` component, our provided values can be refactored as the following:
53
+
Usando nosso componente `MyMap`, nossos valores providos podem ser refatorados da seguinte forma:
54
54
55
55
```vue{7,14-20}
56
56
<!-- src/components/MyMap.vue -->
@@ -67,7 +67,7 @@ export default {
67
67
MyMarker
68
68
},
69
69
setup() {
70
-
provide('location', 'North Pole')
70
+
provide('location', 'Polo Norte')
71
71
provide('geolocation', {
72
72
longitude: 90,
73
73
latitude: 135
@@ -77,16 +77,16 @@ export default {
77
77
</script>
78
78
```
79
79
80
-
## Using Inject
80
+
## Usando Inject
81
81
82
-
When using`inject`in`setup()`, we also need to explicitly import it from `vue`. Once we do so, this allows us to invoke it to define how we want to expose it to our component.
82
+
Ao usar`inject`no`setup()`, também precisamos importá-lo explicitamente de `vue`. Assim que fizermos isso, isso nos permitirá invocá-lo para definir como queremos expô-lo ao nosso componente.
83
83
84
-
The `inject`function takes two parameters:
84
+
A função `inject`leva dois parâmetros:
85
85
86
-
1.The name of the property to inject
87
-
2.A default value (**Optional**)
86
+
1.O nome da propriedade a injetar; e
87
+
2.Um valor padrão (**Opcional**).
88
88
89
-
Using our `MyMarker` component, we can refactor it with the following code:
89
+
Usando nosso componente `MyMarker`, podemos refatorá-lo com o seguinte código:
90
90
91
91
```vue{3,6-14}
92
92
<!-- src/components/MyMarker.vue -->
@@ -95,7 +95,7 @@ import { inject } from 'vue'
95
95
96
96
export default {
97
97
setup() {
98
-
const userLocation = inject('location', 'The Universe')
98
+
const userLocation = inject('location', 'O Universo')
99
99
const userGeolocation = inject('geolocation')
100
100
101
101
return {
@@ -107,13 +107,13 @@ export default {
107
107
</script>
108
108
```
109
109
110
-
## Reactivity
110
+
## Reatividade
111
111
112
-
### Adding Reactivity
112
+
### Adicionando Reatividade
113
113
114
-
To add reactivity between provided and injected values, we can use a [ref](reactivity-fundamentals.html#creating-standalone-reactive-values-as-refs)or[reactive](reactivity-fundamentals.html#declaring-reactive-state) when providing a value.
114
+
Para adicionar reatividade entre os valores providos e injetados, podemos usar uma [ref](reactivity-fundamentals.html#criacao-de-valores-reativos-avulsos-como-refs)ou[reactive](reactivity-fundamentals.html#declarando-estado-reativo) ao prover um valor.
115
115
116
-
Using our `MyMap` component, our code can be updated as follows:
116
+
Usando nosso componente `MyMap`, nosso código pode ser atualizado da seguinte forma:
117
117
118
118
```vue{7,15-22}
119
119
<!-- src/components/MyMap.vue -->
@@ -130,7 +130,7 @@ export default {
130
130
MyMarker
131
131
},
132
132
setup() {
133
-
const location = ref('North Pole')
133
+
const location = ref('Polo Norte')
134
134
const geolocation = reactive({
135
135
longitude: 90,
136
136
latitude: 135
@@ -143,13 +143,13 @@ export default {
143
143
</script>
144
144
```
145
145
146
-
Now, if anything changes in either property, the `MyMarker`component will automatically be updated as well!
146
+
Agora, se alguma coisa mudar em qualquer uma das propriedades, o componente `MyMarker`também será atualizado automaticamente!
147
147
148
-
### Mutating Reactive Properties
148
+
### Mutando Propriedades Reativas
149
149
150
-
When using reactive provide / inject values, **it is recommended to keep any mutations to reactive properties inside of the _provider_whenever possible**.
150
+
Ao usar dados providos e injetados reativos, **é recomendado manter quaisquer mutações nas propriedades reativas dentro do _provider_sempre que possível**.
151
151
152
-
For example, in the event we needed to change the user's location, we would ideally do this inside of our `MyMap` component.
152
+
Por exemplo, no caso de precisarmos alterar a localização do usuário, o ideal seria fazer isso dentro de nosso componente `MyMap`.
153
153
154
154
```vue{28-32}
155
155
<!-- src/components/MyMap.vue -->
@@ -166,7 +166,7 @@ export default {
166
166
MyMarker
167
167
},
168
168
setup() {
169
-
const location = ref('North Pole')
169
+
const location = ref('Polo Norte')
170
170
const geolocation = reactive({
171
171
longitude: 90,
172
172
latitude: 135
@@ -181,14 +181,14 @@ export default {
181
181
},
182
182
methods: {
183
183
updateLocation() {
184
-
this.location = 'South Pole'
184
+
this.location = 'Polo Sul'
185
185
}
186
186
}
187
187
}
188
188
</script>
189
189
```
190
190
191
-
However, there are times where we need to update the data inside of the component where the data is injected. In this scenario, we recommend providing a method that is responsible for mutating the reactive property.
191
+
No entanto, há momentos em que precisamos atualizar os dados dentro do componente onde os dados são injetados. Nesse cenário, recomendamos prover um método que seja responsável por mutar a propriedade reativa.
192
192
193
193
```vue{21-23,27}
194
194
<!-- src/components/MyMap.vue -->
@@ -205,14 +205,14 @@ export default {
205
205
MyMarker
206
206
},
207
207
setup() {
208
-
const location = ref('North Pole')
208
+
const location = ref('Polo Norte')
209
209
const geolocation = reactive({
210
210
longitude: 90,
211
211
latitude: 135
212
212
})
213
213
214
214
const updateLocation = () => {
215
-
location.value = 'South Pole'
215
+
location.value = 'Polo Sul'
216
216
}
217
217
218
218
provide('location', location)
@@ -230,7 +230,7 @@ import { inject } from 'vue'
230
230
231
231
export default {
232
232
setup() {
233
-
const userLocation = inject('location', 'The Universe')
233
+
const userLocation = inject('location', 'O Universo')
Finally, we recommend using `readonly`on provided property if you want to ensure that the data passed through `provide`cannot be mutated by the injected component.
247
+
Por fim, recomendamos o uso de `readonly`na propriedade provida, se você quiser garantir que os dados passados por `provide`não possam sofrer mutação pelo componente injetado.
0 commit comments