mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-08 17:12:28 +08:00
前端和后端源码,合并到一个git仓库中,方便用户下载,避免前后端不匹配的问题
This commit is contained in:
38
jeecgboot-vue3/src/hooks/core/useContext.ts
Normal file
38
jeecgboot-vue3/src/hooks/core/useContext.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import {
|
||||
InjectionKey,
|
||||
provide,
|
||||
inject,
|
||||
reactive,
|
||||
readonly as defineReadonly,
|
||||
// defineComponent,
|
||||
UnwrapRef,
|
||||
} from 'vue';
|
||||
|
||||
export interface CreateContextOptions {
|
||||
readonly?: boolean;
|
||||
createProvider?: boolean;
|
||||
native?: boolean;
|
||||
}
|
||||
|
||||
type ShallowUnwrap<T> = {
|
||||
[P in keyof T]: UnwrapRef<T[P]>;
|
||||
};
|
||||
|
||||
export function createContext<T>(context: any, key: InjectionKey<T> = Symbol(), options: CreateContextOptions = {}) {
|
||||
const { readonly = true, createProvider = false, native = false } = options;
|
||||
|
||||
const state = reactive(context);
|
||||
const provideData = readonly ? defineReadonly(state) : state;
|
||||
!createProvider && provide(key, native ? context : provideData);
|
||||
|
||||
return {
|
||||
state,
|
||||
};
|
||||
}
|
||||
|
||||
export function useContext<T>(key: InjectionKey<T>, native?: boolean): T;
|
||||
export function useContext<T>(key: InjectionKey<T>, defaultValue?: any, native?: boolean): T;
|
||||
|
||||
export function useContext<T>(key: InjectionKey<T> = Symbol(), defaultValue?: any): ShallowUnwrap<T> {
|
||||
return inject(key, defaultValue || {});
|
||||
}
|
||||
Reference in New Issue
Block a user