Преглед изворни кода

fix: 多语言全局方法声明修复

王家程 пре 10 месеци
родитељ
комит
168b61fc27
3 измењених фајлова са 13 додато и 12 уклоњено
  1. 5 4
      src/locale/index.ts
  2. 4 4
      src/pages/index/index.vue
  3. 4 4
      types/shims-uni.d.ts

+ 5 - 4
src/locale/index.ts

@@ -2,9 +2,10 @@
  * @Author: wjc
  * @Date: 2024-07-11 20:29:20
  * @LastEditors: wjc
- * @LastEditTime: 2024-07-12 10:36:18
+ * @LastEditTime: 2024-07-12 16:39:41
  * @Description:
  */
+import type { App } from 'vue'
 import { createI18n } from 'vue-i18n'
 
 import en from './en.json' // 英文
@@ -63,14 +64,14 @@ export function formatI18n(template: string, data?: any) {
   })
 }
 
-export function $$t(key, data?) {
+export function t(key, data?) {
   return formatI18n(getTemplateByKey(key), data)
 }
 
-export function setupI18n(app) {
+export function setupI18n(app: App<Element>) {
   app.use(i18n)
   // 注册全局的多语言函数
-  app.config.globalProperties.$$t = $$t
+  app.config.globalProperties.$tt = t
 }
 
 export default i18n

+ 4 - 4
src/pages/index/index.vue

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2019-08-22 19:41:20
  * @LastEditors: wjc
- * @LastEditTime: 2024-07-12 10:26:56
+ * @LastEditTime: 2024-07-12 16:45:17
  * @Description: 
 -->
 <template>
@@ -10,9 +10,9 @@
   <view class="content">
     <image class="logo" src="/static/logo.png"></image>
     <view class="text-area">
-      <view class="title">多语言文本:{{ $$t('index.custom.icon') }}</view>
-      <view class="title">多语言传参:{{ $$t('index.lang.params', { appName }) }}</view>
-      <view>多语言2: {{ $t('index.custom.icon') }}</view>
+      <view class="title">多语言文本:{{ $tt('index.custom.icon') }}</view>
+      <view class="title">多语言传参:{{ $tt('index.lang.params', { appName }) }}</view>
+      <view>多语言$t: {{ $t('index.custom.icon') }}</view>
     </view>
     <view :class="`i-custom-${icon}`" class="wh-32px text-32px"></view>
     <view>

+ 4 - 4
types/shims-uni.d.ts

@@ -2,16 +2,16 @@
  * @Author: wjc
  * @Date: 2024-06-05 15:57:41
  * @LastEditors: wjc
- * @LastEditTime: 2024-07-12 10:42:01
+ * @LastEditTime: 2024-07-12 16:44:10
  * @Description:
  */
 /// <reference types='@dcloudio/types' />
 import 'vue'
-import { formatI18n } from '@/locale'
 
 declare module '@vue/runtime-core' {
   type Hooks = App.AppInstance & Page.PageInstance
-  interface ComponentCustomOptions extends Hooks {
-    $$t?: typeof formatI18n
+  interface ComponentCustomOptions extends Hooks {}
+  interface ComponentCustomProperties {
+    $tt?: (template: string, data?: any) => string
   }
 }