Skip to content

Commit 0517a16

Browse files
feat: add hasInjectionContext API
Close #2573
1 parent 662c7da commit 0517a16

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/api/composition-api-dependency-injection.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ Provides a value that can be injected by descendant components.
2323
```vue
2424
<script setup>
2525
import { ref, provide } from 'vue'
26-
import { fooSymbol } from './injectionSymbols'
26+
import { countSymbol } from './injectionSymbols'
2727
2828
// provide static value
29-
provide('foo', 'bar')
29+
provide('path', '/project/')
3030
3131
// provide reactive value
3232
const count = ref(0)
3333
provide('count', count)
3434
3535
// provide with Symbol keys
36-
provide(fooSymbol, count)
36+
provide(countSymbol, count)
3737
</script>
3838
```
3939

@@ -81,19 +81,19 @@ Injects a value provided by an ancestor component or the application (via `app.p
8181
```vue
8282
<script setup>
8383
import { inject } from 'vue'
84-
import { fooSymbol } from './injectionSymbols'
84+
import { countSymbol } from './injectionSymbols'
8585
8686
// inject static value without default
87-
const foo = inject('foo')
87+
const path = inject('path')
8888
8989
// inject reactive value
9090
const count = inject('count')
9191
9292
// inject with Symbol keys
93-
const foo2 = inject(fooSymbol)
93+
const count2 = inject(countSymbol)
9494
9595
// inject with default value
96-
const bar = inject('foo', 'default value')
96+
const bar = inject('path', '/default-path')
9797
9898
// inject with function default value
9999
const fn = inject('function', () => {})
@@ -103,6 +103,16 @@ Injects a value provided by an ancestor component or the application (via `app.p
103103
</script>
104104
```
105105

106-
- **See also**
106+
## hasInjectionContext() <sup class="vt-badge" data-text="3.3+" /> {#has-injection-context}
107+
108+
Returns true if [inject()](#inject) can be used without warning about being called in the wrong place (e.g. outside of `setup()`). This method is designed to be used by libraries that want to use `inject()` internally without triggering a warning to the end user.
109+
110+
- **Type**
111+
112+
```ts
113+
function hasInjectionContext(): boolean
114+
```
115+
116+
* **See also**
107117
- [Guide - Provide / Inject](/guide/components/provide-inject)
108118
- [Guide - Typing Provide / Inject](/guide/typescript/composition-api#typing-provide-inject) <sup class="vt-badge ts" />

0 commit comments

Comments
 (0)