Skip to content

injectLocal

Category
Export Size
Last Changed
6 months ago

扩展了 inject,能够调用 provideLocal 在同一组件中提供值。

用法

vue
<script setup>
import { injectLocal, provideLocal } from '@vueuse/core'

provideLocal('MyInjectionKey', 1)
const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
</script>

类型声明

typescript
/**
 * 在 `inject` 的基础上,允许直接调用 `inject` 来获取在同一组件中调用 provide 后的值。
 *
 * @example
 * ```ts
 * injectLocal('MyInjectionKey', 1)
 * const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
 * ```
 */
export declare const injectLocal: typeof inject

Source

SourceDocs

贡献者

丶远方
ZHAO Jin-Xiang
Anthony Fu

变更日志

v10.5.0 on 10/7/2023
cf757 - fix: vue 2 support for provideLocal and injectLocal (#3464)
5d948 - feat(createInjectionState): allow provide and inject in same component (#3387)

Released under the MIT License.