Skip to content

provideLocal

类别
导出体积
227 B
上次更改
5 days ago

扩展了 provide 方法,使其能够通过调用 injectLocal 在同一组件中获取值。

用法

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

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

类型声明

typescript
export type ProvideLocalReturn = void
/**
 * 在 `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

源码

源码文档

贡献者

一纸忘忧
ZHAO Jin-Xiang
Robin
Anthony Fu

更新日志

c1d6e - feat(shared): ensure return types exists (#4659)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
cf757 - fix: vue 2 support for provideLocal and injectLocal (#3464)
5d948 - feat(createInjectionState): allow provide and inject in same component (#3387)