跳到主要内容

provideLocal

分类
导出大小
226 B
上次更改
上个月

扩展了 provide,使其能够在同一个组件中调用 injectLocal 来获取值。

用法

vue
<script setup>
import { 
injectLocal
,
provideLocal
} from '@vueuse/core'
provideLocal
('MyInjectionKey', 1)
const
injectedValue
=
injectLocal
('MyInjectionKey') // injectedValue === 1
</script>

类型声明

ts
export type 
ProvideLocalReturn
= void
/** * On the basis of `provide`, it is allowed to directly call inject to obtain the value after call provide in the same component. * * @example * ```ts * provideLocal('MyInjectionKey', 1) * const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1 * ``` */ export declare function
provideLocal
<
T
,
K
=
LocalProvidedKey
<
T
>>(
key
:
K
,
value
:
K
extends
InjectionKey
<infer
V
> ?
V
:
T
,
):
ProvideLocalReturn

来源

源码文档

贡献者

ZHAO Jin-Xiang
Zhaokun
Arthur Darkstone
Robin
Anthony Fu

更新日志

v14.0.0
51872 - fix(shared): 在 vapor 模式下支持 provideLocal/injectLocal (#5050)
v13.1.0
c1d6e - feat(shared): 确保返回类型存在 (#4659)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.5.0
cf757 - fix: Vue 2 对 provideLocal 和 injectLocal 的支持 (#3464)
5d948 - feat(createInjectionState): 允许在同一个组件中进行 provide 和 inject (#3387)

根据 MIT 许可证发布。