|
23 | 23 | ```vue
|
24 | 24 | <script setup>
|
25 | 25 | import { ref, provide } from 'vue'
|
26 |
| - import { fooSymbol } from './injectionSymbols' |
| 26 | + import { countSymbol } from './injectionSymbols' |
27 | 27 |
|
28 | 28 | // 静的な値を提供
|
29 |
| - provide('foo', 'bar') |
| 29 | + provide('path', '/project/') |
30 | 30 |
|
31 | 31 | // リアクティブな値を提供
|
32 | 32 | const count = ref(0)
|
33 | 33 | provide('count', count)
|
34 | 34 |
|
35 | 35 | // シンボルのキーを使って提供
|
36 |
| - provide(fooSymbol, count) |
| 36 | + provide(countSymbol, count) |
37 | 37 | </script>
|
38 | 38 | ```
|
39 | 39 |
|
|
81 | 81 | ```vue
|
82 | 82 | <script setup>
|
83 | 83 | import { inject } from 'vue'
|
84 |
| - import { fooSymbol } from './injectionSymbols' |
| 84 | + import { countSymbol } from './injectionSymbols' |
85 | 85 |
|
86 | 86 | // デフォルト値なしの静的な値を注入
|
87 |
| - const foo = inject('foo') |
| 87 | + const path = inject('path') |
88 | 88 |
|
89 | 89 | // リアクティブな値を注入
|
90 | 90 | const count = inject('count')
|
91 | 91 |
|
92 | 92 | // シンボルのキーを使って注入
|
93 |
| - const foo2 = inject(fooSymbol) |
| 93 | + const count2 = inject(countSymbol) |
94 | 94 |
|
95 | 95 | // デフォルト値ありで注入
|
96 |
| - const bar = inject('foo', 'default value') |
| 96 | + const bar = inject('path', '/default-path') |
97 | 97 |
|
98 | 98 | // 関数のデフォルト値を使って注入
|
99 | 99 | const fn = inject('function', () => {})
|
|
103 | 103 | </script>
|
104 | 104 | ```
|
105 | 105 |
|
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 | +* **参照** |
107 | 117 | - [ガイド - Provide / Inject](/guide/components/provide-inject)
|
108 | 118 | - [ガイド - Provide / Inject の型付け](/guide/typescript/composition-api#typing-provide-inject) <sup class="vt-badge ts" />
|
0 commit comments