vue-i18n在使用$t时提示类型错误

1. 问题描述

Vue3项目中,使用vue-i18n,在模版中使用$t时,页面可以正常渲染,但是类型报错。

相关依赖版本如下:

json 复制代码
"dependencies": {
    "vue": "^3.4.29",
    "vue-i18n": "^9.14.0",
    // 其它依赖
},

2. 解决方法

手动给 $t 添加类型声明。

创建一个类型声明文件vue-i18n.d.ts,并将其添加到 tsconfig.json 配置文件的 include 配置项中。文件内容如下:

ts 复制代码
export {};

declare module '@vue/runtime-core' {
    // 提供全局属性 `$t`
    interface ComponentCustomProperties {
        $t: (message: string) => string;
    }
}

参考文档

相关推荐
Sheldon一蓑烟雨任平生19 小时前
Sass 星空(Sass + keyframes 实现星空动画)
前端·css·vue3·sass·keyframes
Irene19911 天前
Vue 3 中移除了 $on、$off 和 $once 方法(附:Composables 组合式函数 使用详解)
vue3
Sapphire~3 天前
Vue3-15 html标签和组件上的ref属性 + 接口泛型
vue3
Irene19913 天前
Vue 3 中使用 Mitt 事件总线
vue3·mitt
咸甜适中3 天前
双色球、大乐透兑奖分析小程序(rust_Tauri + Vue3 + sqlite)
爬虫·rust·sqlite·vue3·tauri2
Sapphire~5 天前
Vue3-012 vue2与vue3中的computed
vue3
Sapphire~7 天前
Vue3-11 toRefs 和 toRef
vue3
华玥作者9 天前
uni-app + Vite 项目中使用 @uni-helper/vite-plugin-uni-pages 实现自动路由配置(超详细)
前端·uni-app·vue·vue3·vite
独立开发者阿乐10 天前
Vue3中Markdown解析与渲染的完整解决方案:从安全到性能优化
web安全·性能优化·vue3·前端开发·语法高亮·markdown解析·markdown-it
Sapphire~11 天前
Vue3-10 ref与reactive创建响应式数据的区别
vue3