Skip to content

Commit

Permalink
fix: 修复全局组件不加载语言包问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Sep 15, 2022
1 parent d55f537 commit 33ae6fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/meComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineComponent({
transition: Object as PropType<TransitionProps>,
},
setup(props, { attrs }) {
const loadMessages = useLoadMessages();
const { loadMessages, clearCache } = useLoadMessages();
const componentIs: Ref<any> = ref(undefined);
const key = ref(props.componentKey);
const _attrs = ref(attrs);
Expand All @@ -24,6 +24,7 @@ export default defineComponent({
async (is) => {
if (is) {
localeConfig.loadMessageConfig.componentLoad && (await Promise.allSettled(loadMessages(is as any, false))); // 自动加载语言包
clearCache();
componentIs.value = is;
key.value = props.componentKey;
_attrs.value = attrs;
Expand Down
15 changes: 9 additions & 6 deletions src/locales/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { useI18n, UseI18nOptions } from 'vue-i18n';
import { loadMessage, MessageImport, setLocaleMessage } from './helper';
import { mitter, event } from '@/event';
import { ComponentOptions, VNode } from 'vue';
import { camelize, capitalize, ComponentOptions, VNode } from 'vue';

/**
* useI18n 会自动加载locale语言包(语言包加载为异步执行,如果语言包被加载过则执行时效和同步一致)
Expand Down Expand Up @@ -64,18 +64,21 @@ export const useLoadMessages = () => {
if (instance === null) {
throw new Error('必须在setup中调用');
}
const cache = new Set<any>();
const app = instance.appContext.app;
const loadMessages = (
options: (VNode & { __v_isVNode: true }) | ComponentOptions | string,
isLoading = true,
locale: string | undefined = undefined,
importArr: Array<Promise<any>> = [],
) => {
if (cache.has(options)) {
return importArr;
}
cache.add(options);
if (typeof options === 'string') {
const component = app.component(options);
if (component) {
loadMessages(options, isLoading, locale, importArr);
}
const component = app.component(capitalize(camelize(options)));
loadMessages(component as ComponentOptions, isLoading, locale, importArr);
return importArr;
}
if (options.__v_isVNode) {
Expand All @@ -97,5 +100,5 @@ export const useLoadMessages = () => {
}
return importArr;
};
return loadMessages;
return { loadMessages, clearCache: () => cache.clear() };
};

0 comments on commit 33ae6fe

Please sign in to comment.