Commit c22de5c35b4781322c9ee17ad375ec0af2fe60a7

Authored by vben
1 parent 3509ebec

fix: useI18n type

src/hooks/web/useI18n.ts
1 import { i18n } from '/@/locales/setupI18n'; 1 import { i18n } from '/@/locales/setupI18n';
2 2
3 -export function useI18n(namespace?: string) { 3 +type I18nGlobalTranslation = {
  4 + (key: string): string;
  5 + (key: string, locale: string): string;
  6 + (key: string, locale: string, list: unknown[]): string;
  7 + (key: string, locale: string, named: Record<string, unknown>): string;
  8 + (key: string, list: unknown[]): string;
  9 + (key: string, named: Record<string, unknown>): string;
  10 +};
  11 +
  12 +type I18nTranslationRestParameters = [string, any];
  13 +
  14 +export function useI18n(
  15 + namespace?: string
  16 +): {
  17 + t: I18nGlobalTranslation;
  18 +} {
4 function getKey(key: string) { 19 function getKey(key: string) {
5 if (!namespace) { 20 if (!namespace) {
6 return key; 21 return key;
@@ -22,9 +37,9 @@ export function useI18n(namespace?: string) { @@ -22,9 +37,9 @@ export function useI18n(namespace?: string) {
22 37
23 const { t, ...methods } = i18n.global; 38 const { t, ...methods } = i18n.global;
24 39
25 - const tFn: typeof t = (key: string, ...arg: any) => { 40 + const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => {
26 if (!key) return ''; 41 if (!key) return '';
27 - return t(getKey(key), ...(arg as Parameters<typeof t>)); 42 + return t(getKey(key), ...(arg as I18nTranslationRestParameters));
28 }; 43 };
29 return { 44 return {
30 ...methods, 45 ...methods,
src/views/demo/feat/img-preview/index.vue
1 <template> 1 <template>
2 <PageWrapper title="图片预览示例"> 2 <PageWrapper title="图片预览示例">
  3 + <ImagePreview :imageList="imgList" />
3 <Alert message="有预览图" type="info" /> 4 <Alert message="有预览图" type="info" />
4 <div class="flex justify-center mt-4"> 5 <div class="flex justify-center mt-4">
5 <img :src="img" v-for="img in imgList" :key="img" class="mr-2" @click="handleClick(img)" /> 6 <img :src="img" v-for="img in imgList" :key="img" class="mr-2" @click="handleClick(img)" />
@@ -11,7 +12,7 @@ @@ -11,7 +12,7 @@
11 <script lang="ts"> 12 <script lang="ts">
12 import { defineComponent } from 'vue'; 13 import { defineComponent } from 'vue';
13 import { Alert } from 'ant-design-vue'; 14 import { Alert } from 'ant-design-vue';
14 - import { createImgPreview } from '/@/components/Preview/index'; 15 + import { createImgPreview, ImagePreview } from '/@/components/Preview/index';
15 import { PageWrapper } from '/@/components/Page'; 16 import { PageWrapper } from '/@/components/Page';
16 17
17 const imgList: string[] = [ 18 const imgList: string[] = [
@@ -20,7 +21,7 @@ @@ -20,7 +21,7 @@
20 'https://picsum.photos/id/68/346/216', 21 'https://picsum.photos/id/68/346/216',
21 ]; 22 ];
22 export default defineComponent({ 23 export default defineComponent({
23 - components: { Alert, PageWrapper }, 24 + components: { Alert, PageWrapper, ImagePreview },
24 setup() { 25 setup() {
25 function handleClick(img: string) { 26 function handleClick(img: string) {
26 createImgPreview({ imageList: [img] }); 27 createImgPreview({ imageList: [img] });