2022-05-10 08:18:44 +00:00
|
|
|
import { inject, provide } from 'vue';
|
|
|
|
|
2022-05-11 13:52:51 +00:00
|
|
|
function createContext<T>(defaultValue?: T) {
|
2022-05-10 08:18:44 +00:00
|
|
|
const contextKey = Symbol('contextKey');
|
|
|
|
const useProvide = (props: T) => {
|
|
|
|
provide(contextKey, props);
|
|
|
|
};
|
|
|
|
const useInject = () => {
|
2022-05-11 13:52:51 +00:00
|
|
|
return inject(contextKey, defaultValue as T) || ({} as T);
|
2022-05-10 08:18:44 +00:00
|
|
|
};
|
|
|
|
return {
|
|
|
|
useProvide,
|
|
|
|
useInject,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createContext;
|