You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt 3中@nuxtjs/i18n的__VLS_ctx.$t类型未知错误求助

解决Nuxt3中__VLS_ctx.$t类型未知的问题

以下是无需替换模板中$t即可解决该错误的方案:

1. 修复TypeScript类型声明

  • 在项目根目录创建types/i18n.d.ts文件,添加内容扩展Vue全局类型:
import type { Composer } from '@nuxtjs/i18n'

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $t: Composer['t']
    $i18n: Composer
  }
}

export {}
  • 检查tsconfig.json,确保types字段包含@nuxtjs/i18n,示例:
{
  "extends": "./.nuxt/tsconfig.json",
  "types": ["@nuxtjs/i18n"]
}

2. 调整Volar(VLS)配置

在.vscode/settings.json中添加配置,强制Volar识别Nuxt全局上下文:

{
  "volar.tsPlugin": true,
  "volar.tsPluginStatus": "enable",
  "vue.intellisense.types": ["@nuxtjs/i18n"]
}

3. 清理各类缓存

  • 执行npx nuxt clean清除Nuxt缓存
  • 删除项目根目录的.nuxt、.output和node_modules/.cache文件夹
  • 重启VS Code确保缓存完全刷新

4. 验证Nuxt i18n配置

确认nuxt.config.ts中i18n模块配置正确,示例:

export default defineNuxtConfig({
  modules: ['@nuxtjs/i18n'],
  i18n: {
    locales: ['en', 'zh'],
    defaultLocale: 'en',
    vueI18n: {
      legacy: false,
      locale: 'en',
      messages: {
        en: { /* 英文翻译内容 */ },
        zh: { /* 中文翻译内容 */ }
      }
    }
  }
})

注意确保legacy: false,适配Nuxt3的组合式API模式,避免类型冲突。

内容的提问来源于stack exchange,提问作者Kirill Donenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 17:29:53