vue-vben-admin/src/hooks/core/useContext.ts

23 lines
593 B
TypeScript
Raw Normal View History

2020-11-24 22:59:29 +08:00
import { InjectionKey, provide, inject, reactive, readonly } from 'vue';
export const createContext = <T>(
context: any,
contextInjectKey: InjectionKey<T> = Symbol(),
_readonly = true
) => {
const state = reactive({
...context,
});
const provideData = _readonly ? readonly(state) : state;
provide(contextInjectKey, provideData);
};
export const useContext = <T>(
contextInjectKey: InjectionKey<T> = Symbol(),
defaultValue?: any,
_readonly = true
): T => {
const state = inject(contextInjectKey, defaultValue || {});
return _readonly ? readonly(state) : state;
};