Vue3 TypeScript报错:Module vue has no exported... 类型查找失败求助
修复TypeScript无法找到Vue类型的报错
梳理依赖配置
Vue 3自带类型定义,无需额外安装@types/vue,如果之前装过Vue2版本的@types/vue,先卸载:npm uninstall @types/vue再确保核心依赖为最新版本:
npm install vue@latest npm install -D typescript @vitejs/plugin-vue修正tsconfig配置
打开项目的tsconfig.json,确保关键配置项正确:{ "compilerOptions": { "target": "ESNext", "module": "ESNext", "moduleResolution": "Node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "isolatedModules": true, "esModuleInterop": true, "lib": ["ESNext", "DOM"], "types": ["vite/client"] }, "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.node.json" }] }重点检查:
types数组需包含对应构建工具的环境类型(如Vite项目需vite/client)include需覆盖所有.vue和TypeScript文件- 若存在
tsconfig.node.json,确保其仅针对Node环境配置,不干扰主TS配置
添加Vue文件类型声明
在src目录下创建shims-vue.d.ts文件,内容如下:declare module '*.vue' { import type { DefineComponent } from 'vue' const component: DefineComponent<{}, {}, any> export default component }该文件用于告知TypeScript如何识别
.vue组件类型重启TypeScript服务
在VS Code中按下Ctrl+Shift+P(Windows)/Cmd+Shift+P(Mac),选择TypeScript: Restart TS Server,让配置修改生效
内容的提问来源于stack exchange,提问作者passionateLearner
相关产品推荐
相关产品推荐

