28 lines
634 B
TypeScript
28 lines
634 B
TypeScript
import { computed, getCurrentInstance, inject, provide, unref } from 'vue'
|
|
import type { MaybeRef, Ref } from 'vue'
|
|
|
|
export function useGlobalConfig(
|
|
key: string,
|
|
defaultValue: unknown = undefined,
|
|
): Ref<unknown> {
|
|
const config = inject('GLOBAL_CONFIG') as MaybeRef<unknown>
|
|
|
|
if (key)
|
|
return computed(() => unref(config)?.[key] ?? defaultValue)
|
|
else
|
|
return config
|
|
}
|
|
|
|
export function provideGlobalConfig(config, app) {
|
|
const inSetup = !!getCurrentInstance()
|
|
|
|
const provideFn = app?.provide ?? (inSetup ? provide : undefined)
|
|
|
|
if (!provideFn)
|
|
return
|
|
|
|
provideFn('GLOBAL_CONFIG', config)
|
|
|
|
return config
|
|
}
|