Skip to content

Commit 8bf845f

Browse files
committed
ts + composition-api
1 parent 6af655d commit 8bf845f

File tree

3 files changed

+312
-164
lines changed

3 files changed

+312
-164
lines changed

src/guide/components/provide-inject.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export default {
9595
}
9696
```
9797

98-
However, do note this does **not** make the injection reactive. We will discuss [making injections reactive](#injection-reactivity) below.
98+
However, do note this does **not** make the injection reactive. We will discuss [making injections reactive](#working-with-reactivity) below.
9999

100100
</div>
101101

@@ -175,9 +175,7 @@ export default {
175175

176176
[Full provide + inject example](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmltcG9ydCBDaGlsZCBmcm9tICcuL0NoaWxkLnZ1ZSdcblxuZXhwb3J0IGRlZmF1bHQge1xuICBjb21wb25lbnRzOiB7IENoaWxkIH0sXG4gIHByb3ZpZGUoKSB7XG4gICAgcmV0dXJuIHtcbiAgICAgIG1lc3NhZ2U6ICdoZWxsbydcbiAgICB9XG4gIH1cbn1cbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxDaGlsZCAvPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59IiwiQ2hpbGQudnVlIjoiPHNjcmlwdD5cbmltcG9ydCBHcmFuZENoaWxkIGZyb20gJy4vR3JhbmRDaGlsZC52dWUnXG5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgY29tcG9uZW50czoge1xuICAgIEdyYW5kQ2hpbGRcbiAgfVxufVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPEdyYW5kQ2hpbGQgLz5cbjwvdGVtcGxhdGU+IiwiR3JhbmRDaGlsZC52dWUiOiI8c2NyaXB0PlxuZXhwb3J0IGRlZmF1bHQge1xuICBpbmplY3Q6IFsnbWVzc2FnZSddXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8cD5cbiAgICBNZXNzYWdlIHRvIGdyYW5kIGNoaWxkOiB7eyBtZXNzYWdlIH19XG4gIDwvcD5cbjwvdGVtcGxhdGU+In0=)
177177

178-
179-
180-
### Injection Aliasing *
178+
### Injection Aliasing \*
181179

182180
When using the array syntax for `inject`, the injected properties are exposed on the component instance using the same key. In the example above, the property was provided under the key `"message"`, and injected as `this.message`. The local key is the same as the injection key.
183181

@@ -238,7 +236,61 @@ export default {
238236
}
239237
```
240238

241-
## Injection Reactivity *
239+
</div>
240+
241+
## Working with Reactivity
242+
243+
<div class="composition-api">
244+
245+
When using reactive provide / inject values, **it is recommended to keep any mutations to reactive state inside of the _provider_ whenever possible**. This ensures that the provided state and its possible mutations are co-located in the same component, making it easier to maintain in the future.
246+
247+
There may be times where we need to update the data from a injector component. In such cases, we recommend providing a method that is responsible for mutating the state:
248+
249+
```vue{7-9,13}
250+
<!-- inside provider component -->
251+
<script setup>
252+
import { provide, ref } from 'vue'
253+
254+
const ___location = ref('North Pole')
255+
256+
function updateLocation() {
257+
___location.value = 'South Pole'
258+
}
259+
260+
provide('___location', {
261+
___location,
262+
updateLocation
263+
})
264+
</script>
265+
```
266+
267+
```vue{5}
268+
<!-- in injector component -->
269+
<script setup>
270+
import { inject } from 'vue'
271+
272+
const { ___location, updateLocation } = inject('___location')
273+
</script>
274+
275+
<template>
276+
<button @click="updateLocation">{{ ___location }}</button>
277+
</template>
278+
```
279+
280+
Finally, you can wrap the provided value with [`readonly()`](/api/reactivity-core.html#readonly) if you want to ensure that the data passed through `provide` cannot be mutated by the injected component.
281+
282+
```vue
283+
<script setup>
284+
import { ref, provide, readonly } from 'vue'
285+
286+
const count = ref(0)
287+
provide('read-only-count', readonly(count))
288+
</script>
289+
```
290+
291+
</div>
292+
293+
<div class="options-api">
242294

243295
In order to make injections reactively linked to the provider, we need to provide a computed property using the [computed()](/api/reactivity-core.html#computed) function:
244296

@@ -286,7 +338,9 @@ export const myInjectionKey = Symbol()
286338
import { provide } from 'vue'
287339
import { myInjectionKey } from './keys.js'
288340

289-
provide(myInjectionKey, { /* data to provide */ })
341+
provide(myInjectionKey, {
342+
/* data to provide */
343+
})
290344
```
291345

292346
```js
@@ -310,7 +364,9 @@ import { myInjectionKey } from './keys.js'
310364
export default {
311365
provide() {
312366
return {
313-
[myInjectionKey]: { /* data to provide */ }
367+
[myInjectionKey]: {
368+
/* data to provide */
369+
}
314370
}
315371
}
316372
}

0 commit comments

Comments
 (0)