mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-21 02:56:38 +08:00
22 lines
447 B
TypeScript
22 lines
447 B
TypeScript
import { inject, provide } from 'vue';
|
|
import type { InjectionKey } from 'vue';
|
|
|
|
/** 创建共享上下文状态 */
|
|
export default function useContext<T>(contextName = 'context') {
|
|
const injectKey: InjectionKey<T> = Symbol(contextName);
|
|
|
|
function useProvide(context: T) {
|
|
provide(injectKey, context);
|
|
return context;
|
|
}
|
|
|
|
function useInject() {
|
|
return inject(injectKey) as T;
|
|
}
|
|
|
|
return {
|
|
useProvide,
|
|
useInject
|
|
};
|
|
}
|