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
相关产品推荐
相关产品推荐

