Skip to content

Commit 22c363d

Browse files
authored
feat: add hasInjectionContext API (#1688)
1 parent 8eee9e9 commit 22c363d

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 @@
2323
```vue
2424
<script setup>
2525
import { ref, provide } from 'vue'
26-
import { fooSymbol } from './injectionSymbols'
26+
import { countSymbol } from './injectionSymbols'
2727
2828
// 静的な値を提供
29-
provide('foo', 'bar')
29+
provide('path', '/project/')
3030
3131
// リアクティブな値を提供
3232
const count = ref(0)
3333
provide('count', count)
3434
3535
// シンボルのキーを使って提供
36-
provide(fooSymbol, count)
36+
provide(countSymbol, count)
3737
</script>
3838
```
3939

@@ -81,19 +81,19 @@
8181
```vue
8282
<script setup>
8383
import { inject } from 'vue'
84-
import { fooSymbol } from './injectionSymbols'
84+
import { countSymbol } from './injectionSymbols'
8585
8686
// デフォルト値なしの静的な値を注入
87-
const foo = inject('foo')
87+
const path = inject('path')
8888
8989
// リアクティブな値を注入
9090
const count = inject('count')
9191
9292
// シンボルのキーを使って注入
93-
const foo2 = inject(fooSymbol)
93+
const count2 = inject(countSymbol)
9494
9595
// デフォルト値ありで注入
96-
const bar = inject('foo', 'default value')
96+
const bar = inject('path', '/default-path')
9797
9898
// 関数のデフォルト値を使って注入
9999
const fn = inject('function', () => {})
@@ -103,6 +103,16 @@
103103
</script>
104104
```
105105

106-
- **参照**
106+
## hasInjectionContext() <sup class="vt-badge" data-text="3.3+" /> {#has-injection-context}
107+
108+
[inject()](#inject) が警告なしで使用できる場合に true を返します(`setup()` の外側など、間違った場所で呼び出されたという警告)。このメソッドは、エンドユーザーに警告を出すことなく、内部的に `inject()` を使用したいライブラリーが使用するように設計されています。
109+
110+
- ****
111+
112+
```ts
113+
function hasInjectionContext(): boolean
114+
```
115+
116+
* **参照**
107117
- [ガイド - Provide / Inject](/guide/components/provide-inject)
108118
- [ガイド - Provide / Inject の型付け](/guide/typescript/composition-api#typing-provide-inject) <sup class="vt-badge ts" />

0 commit comments

Comments
 (0)