📄 provideLocal¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 6 |
| 📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/shared/provideLocal/index.ts
📦 Imports¶
| Name | Source |
|---|---|
InjectionKey |
vue |
LocalProvidedKey |
./map |
getCurrentInstance |
vue |
getCurrentScope |
vue |
provide |
vue |
localProvidedStateMap |
./map |
Functions¶
provideLocal(key: K, value: K extends InjectionKey<infer V> ? V : T): ProvideLocalReturn¶
On the basis of provide, it is allowed to directly call inject to obtain the value after call provide in the same component.
Examples:
```ts
provideLocal('MyInjectionKey', 1)
const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
<details><summary>Raw JSDoc</summary>
```typescript
/**
* 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
* ```
*/
Calls:
getCurrentInstance (from vue)getCurrentScope (from vue)localProvidedStateMap.haslocalProvidedStateMap.setObject.createlocalProvidedStateMap.getprovide (from vue)
Internal Comments:
Code
export function provideLocal<T, K = LocalProvidedKey<T>>(key: K, value: K extends InjectionKey<infer V> ? V : T): ProvideLocalReturn {
const instance = getCurrentInstance()?.proxy
const owner = instance ?? getCurrentScope()
if (owner == null)
throw new Error('provideLocal must be called in setup')
if (!localProvidedStateMap.has(owner))
localProvidedStateMap.set(owner, Object.create(null))
const localProvidedState = localProvidedStateMap.get(owner)!
// @ts-expect-error allow InjectionKey as key
localProvidedState[key] = value
return provide<T, K>(key, value)
}
Type Aliases¶
ProvideLocalReturn¶
Generated by Syntax Scribe