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

LunarVim中.ts导入.vue报错:无法找到模块或类型声明怎么办?

解决.ts导入.vue模块的类型声明错误
  • 添加Vue类型声明文件
    在项目根目录创建vue-shim.d.ts文件,写入以下内容:

    declare module '*.vue' {
      import type { DefineComponent } from 'vue'
      const component: DefineComponent<{}, {}, any>
      export default component
    }
    
  • 修正tsconfig.json配置
    确保include字段包含.vue文件,示例配置:

    {
      "compilerOptions": {
        "resolveJsonModule": true,
        "esModuleInterop": true
        // 保留原有其他配置
      },
      "include": [
        "src/**/*.ts",
        "src/**/*.d.ts",
        "src/**/*.vue"
      ]
    }
    
  • 配置Volar接管模式
    在LunarVim配置文件(如~/.config/lvim/config.lua)中添加:

    -- 让LunarVim自动配置volar
    lvim.lsp.automatic_configuration.skipped_servers = vim.tbl_filter(
      function(server) return server ~= "volar" end,
      lvim.lsp.automatic_configuration.skipped_servers
    )
    
    require("lvim.lsp.manager").setup("volar", {
      filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact", "vue" },
      init_options = {
        typescript = { serverPath = vim.fn.exepath("typescript-language-server") }
      },
      on_new_config = function(new_config, new_root_dir)
        -- 指向项目本地的typescript包
        new_config.init_options.typescript.tsdk = new_root_dir .. "/node_modules/typescript/lib"
      end
    })
    

    此配置让Volar接管Vue与TS的交叉类型检查,避免tsserver无法识别.vue模块。

  • 重启生效
    执行:LspRestart命令重启所有LSP服务,或直接重启LunarVim。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:13:29