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