Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hasInjectionContext API #1688

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/api/composition-api-dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
```vue
<script setup>
import { ref, provide } from 'vue'
import { fooSymbol } from './injectionSymbols'
import { countSymbol } from './injectionSymbols'

// 静的な値を提供
provide('foo', 'bar')
provide('path', '/project/')

// リアクティブな値を提供
const count = ref(0)
provide('count', count)

// シンボルのキーを使って提供
provide(fooSymbol, count)
provide(countSymbol, count)
</script>
```

Expand Down Expand Up @@ -81,19 +81,19 @@
```vue
<script setup>
import { inject } from 'vue'
import { fooSymbol } from './injectionSymbols'
import { countSymbol } from './injectionSymbols'

// デフォルト値なしの静的な値を注入
const foo = inject('foo')
const path = inject('path')

// リアクティブな値を注入
const count = inject('count')

// シンボルのキーを使って注入
const foo2 = inject(fooSymbol)
const count2 = inject(countSymbol)

// デフォルト値ありで注入
const bar = inject('foo', 'default value')
const bar = inject('path', '/default-path')

// 関数のデフォルト値を使って注入
const fn = inject('function', () => {})
Expand All @@ -103,6 +103,16 @@
</script>
```

- **参照**
## hasInjectionContext() <sup class="vt-badge" data-text="3.3+" /> {#has-injection-context}

[inject()](#inject) が警告なしで使用できる場合に true を返します(`setup()` の外側など、間違った場所で呼び出されたという警告)。このメソッドは、エンドユーザーに警告を出すことなく、内部的に `inject()` を使用したいライブラリーが使用するように設計されています。

- **型**

```ts
function hasInjectionContext(): boolean
```

* **参照**
- [ガイド - Provide / Inject](/guide/components/provide-inject)
- [ガイド - Provide / Inject の型付け](/guide/typescript/composition-api#typing-provide-inject) <sup class="vt-badge ts" />