soybean-admin/src/hooks/common/useContext.ts
2022-04-01 14:47:57 +08:00

21 lines
427 B
TypeScript

import { provide, inject } 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);
}
function useInject() {
return inject(injectKey) as T;
}
return {
useProvide,
useInject
};
}