🐛(i18n): 修复按需加载下 i18n 的注册问题

This commit is contained in:
sight 2022-05-15 17:03:00 +08:00
parent 334699b210
commit 5a05c3a621
7 changed files with 24 additions and 11 deletions

View File

@ -8,7 +8,7 @@ export default {
import "./index.less"; import "./index.less";
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
import { useSlots } from "vue"; import { useSlots } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "../../language";
const { t } = useI18n(); const { t } = useI18n();
const slots = useSlots(); const slots = useSlots();

View File

@ -7,7 +7,7 @@ export default {
<script setup lang="ts"> <script setup lang="ts">
import "./index.less"; import "./index.less";
import { Ref, ref, watch, useSlots, computed } from "vue"; import { Ref, ref, watch, useSlots, computed } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "../../language";
export interface LayPageProps { export interface LayPageProps {
total: number; total: number;

View File

@ -5,6 +5,7 @@ import "@layui/layer-vue/lib/index.css";
import "@layui/icons-vue/lib/index.css"; import "@layui/icons-vue/lib/index.css";
import { layer } from "@layui/layer-vue"; import { layer } from "@layui/layer-vue";
import layerInstall from "@layui/layer-vue"; import layerInstall from "@layui/layer-vue";
import i18n from "./language";
import LayBacktop from "./component/backTop/index"; import LayBacktop from "./component/backTop/index";
import LayAvatar from "./component/avatar/index"; import LayAvatar from "./component/avatar/index";
@ -169,6 +170,7 @@ const install = (app: App, options?: InstallOptions): void => {
for (const key of Object.keys(components)) { for (const key of Object.keys(components)) {
app.use(components[key], options); app.use(components[key], options);
} }
app.use(i18n);
app.use(layerInstall); app.use(layerInstall);
}; };

View File

@ -1,4 +1,5 @@
import { createI18n } from "vue-i18n"; import { getCurrentInstance } from "vue";
import { createI18n, useI18n as __useI18n__ } from "vue-i18n";
import zh_CN from "./locales/zh_CN"; import zh_CN from "./locales/zh_CN";
import en_US from "./locales/en_US"; import en_US from "./locales/en_US";
@ -11,4 +12,18 @@ const i18n = createI18n({
}, },
}); });
export function useI18n() {
let i18nInstance;
const app = getCurrentInstance()?.appContext.app;
try {
i18nInstance = __useI18n__();
} catch (e) {
app?.use(i18n);
i18nInstance = __useI18n__();
}
return i18nInstance;
}
export default i18n; export default i18n;

View File

@ -6,7 +6,7 @@ export default {
<script setup lang="ts"> <script setup lang="ts">
import { watch } from "vue"; import { watch } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "../language";
import { import {
Theme, Theme,
DynamicThemeFix, DynamicThemeFix,

View File

@ -1,15 +1,11 @@
import type { App, Plugin } from "vue"; import { App, Plugin } from "vue";
import i18n from "../language";
export type WithInstallType<T> = T & Plugin; export type WithInstallType<T> = T & Plugin;
export const withInstall = <T>(comp: T): T & Plugin => { export const withInstall = <T>(comp: T): T & Plugin => {
const component = comp as any; const component = comp as any;
component.install = (app: App | any) => { component.install = (app: App) => {
if (!app.__VUE_I18N_SYMBOL__) {
app.use(i18n);
}
app.component(component.name, comp); app.component(component.name, comp);
}; };

View File

@ -75,7 +75,7 @@ export default {
} }
``` ```
::: describe LayuiVueResolver 选项类型声明 ::: describe LayuiVueResolver 类型声明
::: :::
```ts ```ts