mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-11-30 02:57:50 +08:00
fix(StyleContext): fix style cache loaded repeat (#7029)
Co-authored-by: unknown <aibayanyu20>
This commit is contained in:
parent
7786a81e76
commit
61e5746b0d
@ -1,5 +1,13 @@
|
||||
import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue';
|
||||
import { provide, defineComponent, unref, inject, watch, shallowRef } from 'vue';
|
||||
import {
|
||||
provide,
|
||||
defineComponent,
|
||||
unref,
|
||||
inject,
|
||||
watch,
|
||||
shallowRef,
|
||||
getCurrentInstance,
|
||||
} from 'vue';
|
||||
import CacheEntity from './Cache';
|
||||
import type { Linter } from './linters/interface';
|
||||
import type { Transformer } from './transformers/interface';
|
||||
@ -80,6 +88,27 @@ const StyleContextKey: InjectionKey<ShallowRef<Partial<StyleContextProps>>> =
|
||||
Symbol('StyleContextKey');
|
||||
|
||||
export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
|
||||
|
||||
// fix: https://github.com/vueComponent/ant-design-vue/issues/7023
|
||||
const getCache = () => {
|
||||
const instance = getCurrentInstance();
|
||||
let cache: CacheEntity;
|
||||
if (instance && instance.appContext) {
|
||||
const globalCache = instance.appContext?.config?.globalProperties?.__ANTDV_CSSINJS_CACHE__;
|
||||
if (globalCache) {
|
||||
cache = globalCache;
|
||||
} else {
|
||||
cache = createCache();
|
||||
if (instance.appContext.config.globalProperties) {
|
||||
instance.appContext.config.globalProperties.__ANTDV_CSSINJS_CACHE__ = cache;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cache = createCache();
|
||||
}
|
||||
return cache;
|
||||
};
|
||||
|
||||
const defaultStyleContext: StyleContextProps = {
|
||||
cache: createCache(),
|
||||
defaultCache: true,
|
||||
@ -87,7 +116,8 @@ const defaultStyleContext: StyleContextProps = {
|
||||
};
|
||||
// fix: https://github.com/vueComponent/ant-design-vue/issues/6912
|
||||
export const useStyleInject = () => {
|
||||
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache: createCache() }));
|
||||
const cache = getCache();
|
||||
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache }));
|
||||
};
|
||||
export const useStyleProvider = (props: UseStyleProviderProps) => {
|
||||
const parentContext = useStyleInject();
|
||||
|
Loading…
Reference in New Issue
Block a user