🐛(i18n): 修复按需加载下 i18n 的注册问题
This commit is contained in:
		
							parent
							
								
									334699b210
								
							
						
					
					
						commit
						5a05c3a621
					
				| @ -8,7 +8,7 @@ export default { | ||||
| import "./index.less"; | ||||
| import { LayIcon } from "@layui/icons-vue"; | ||||
| import { useSlots } from "vue"; | ||||
| import { useI18n } from "vue-i18n"; | ||||
| import { useI18n }  from "../../language"; | ||||
| 
 | ||||
| const { t } = useI18n(); | ||||
| const slots = useSlots(); | ||||
|  | ||||
| @ -7,7 +7,7 @@ export default { | ||||
| <script setup lang="ts"> | ||||
| import "./index.less"; | ||||
| import { Ref, ref, watch, useSlots, computed } from "vue"; | ||||
| import { useI18n } from "vue-i18n"; | ||||
| import { useI18n }  from "../../language"; | ||||
| 
 | ||||
| export interface LayPageProps { | ||||
|   total: number; | ||||
|  | ||||
| @ -5,6 +5,7 @@ import "@layui/layer-vue/lib/index.css"; | ||||
| import "@layui/icons-vue/lib/index.css"; | ||||
| import { layer } from "@layui/layer-vue"; | ||||
| import layerInstall from "@layui/layer-vue"; | ||||
| import i18n from "./language"; | ||||
| 
 | ||||
| import LayBacktop from "./component/backTop/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)) { | ||||
|     app.use(components[key], options); | ||||
|   } | ||||
|   app.use(i18n); | ||||
|   app.use(layerInstall); | ||||
| }; | ||||
| 
 | ||||
|  | ||||
| @ -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 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; | ||||
|  | ||||
| @ -6,7 +6,7 @@ export default { | ||||
| 
 | ||||
| <script setup lang="ts"> | ||||
| import { watch } from "vue"; | ||||
| import { useI18n } from "vue-i18n"; | ||||
| import { useI18n }  from "../language"; | ||||
| import { | ||||
|   Theme, | ||||
|   DynamicThemeFix, | ||||
|  | ||||
| @ -1,15 +1,11 @@ | ||||
| import type { App, Plugin } from "vue"; | ||||
| import i18n from "../language"; | ||||
| import { App, Plugin } from "vue"; | ||||
| 
 | ||||
| export type WithInstallType<T> = T & Plugin; | ||||
| 
 | ||||
| export const withInstall = <T>(comp: T): T & Plugin => { | ||||
|   const component = comp as any; | ||||
| 
 | ||||
|   component.install = (app: App | any) => { | ||||
|     if (!app.__VUE_I18N_SYMBOL__) { | ||||
|       app.use(i18n); | ||||
|     } | ||||
|   component.install = (app: App) => { | ||||
|     app.component(component.name, comp); | ||||
|   }; | ||||
| 
 | ||||
|  | ||||
| @ -75,7 +75,7 @@ export default { | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| ::: describe LayuiVueResolver 选项类型声明 | ||||
| ::: describe LayuiVueResolver 类型声明 | ||||
| ::: | ||||
| 
 | ||||
| ```ts | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user