initial
This commit is contained in:
27
layers/ui/composables/use-global-config.ts
Normal file
27
layers/ui/composables/use-global-config.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user