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;
    }
}

参考文档

相关推荐
海市公约1 天前
Vue3组合式API中watch传值生命周期与自定义Hook实战
vue3·生命周期·watch·props·组件通信·defineexpose·自定义hook
海市公约2 天前
Vue3组合式API与响应式系统核心机制详解
vue3·computed·reactive·ref·响应式系统·composition api·script setup
小茴香3533 天前
Vue3路由权限动态管理
前端·前端框架·vue3
暗冰ཏོ7 天前
《2026 Vue2 + Vue3 完整学习指南:基础语法、路由缓存、登录拦截、项目实战与面试题》
前端·vue.js·vue·vue3·vue2
曲幽8 天前
写页面时别再把 Element Plus 整个搬进来啦!Vue3按需加载的坑我帮你踩平了
vue3·web·vite·icon·element plus·vs code·import·unplugin
小云小白9 天前
若依-vue3 把深色版本改成天蓝色-含登录页
vue3·若依·天蓝色
曲幽10 天前
FastApiAdmin 后端接口开发好了,前端管理界面怎么调用与显示?
python·vue3·api·fastapi·web·ant design·view·menu·frontend
曲幽13 天前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
Liu.77414 天前
vue3bug收录
vue3
小云小白23 天前
高性能 v-html 弹窗实现:Vue3 + Element Plus 最佳实践
vue3·弹窗·v-html