Lzh on GitHub

扩展了 provide,增加了在 同一组件 中调用 injectLocal 来获取值的能力。

使用

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

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

类型声明

export type ProvideLocalReturn = void
/**
 * 在 provide 的基础上,允许在同一个组件中调用 provide 之后直接调用 inject 来获取值。
 *
 * @example
 * ```ts
 * provideLocal('MyInjectionKey', 1)
 * const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
 * ```
*/
export declare function provideLocal<T, K = InjectionKey<T> | string | number>(
key: K,
value: K extends InjectionKey<infer V> ? V : T,
): ProvideLocalReturn