vue-admin-core
Version:
A Component Library for Vue 3
49 lines (46 loc) • 1.59 kB
JavaScript
import { ref, getCurrentInstance, inject, computed, provide, unref } from 'vue';
import { keysOf } from 'element-plus/es/utils/objects';
import { configProviderContextKey } from '../constants.mjs';
const globalConfig = ref();
function useGlobalConfig(key, defaultValue = void 0) {
const config = getCurrentInstance() ? inject(configProviderContextKey, globalConfig) : globalConfig;
if (key) {
return computed(() => {
var _a, _b;
return (_b = (_a = config.value) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
});
} else {
return config;
}
}
const provideGlobalConfig = (config, app, global = false) => {
var _a;
const inSetup = !!getCurrentInstance();
const oldConfig = inSetup ? useGlobalConfig() : void 0;
const provideFn = (_a = app == null ? void 0 : app.provide) != null ? _a : inSetup ? provide : void 0;
if (!provideFn) {
return;
}
const context = computed(() => {
const cfg = unref(config);
if (!(oldConfig == null ? void 0 : oldConfig.value))
return cfg;
return mergeConfig(oldConfig.value, cfg);
});
provideFn(configProviderContextKey, context);
if (global || !globalConfig.value) {
globalConfig.value = context.value;
}
return context;
};
const mergeConfig = (a, b) => {
var _a;
const keys = [.../* @__PURE__ */ new Set([...keysOf(a), ...keysOf(b)])];
const obj = {};
for (const key of keys) {
obj[key] = (_a = b[key]) != null ? _a : a[key];
}
return obj;
};
export { globalConfig, provideGlobalConfig, useGlobalConfig };
//# sourceMappingURL=use-global-config.mjs.map