Vscode开发vue+ts项目 报错:找不到模块或其相应的类型声明

报错:找不到模块或其相应的类型声明。(Vue 3 can not find module)

报错原因:typescript 只能理解 .ts 文件,无法理解 .vue文件

解决方法:在项目根目录下创建一个后缀为 vue.d.ts 的文件,并写入以下内容:

1
2
3
4
5
// 以下两种方案二选一
// 方案一
declare module "*.vue" { import Vue from "vue"; export default Vue; }
// 方案二
declare module '*.vue' { import { defineComponent } from 'vue'; const Component: ReturnType<typeof defineComponent>; export default Component }

然后在tsconfig.json中写入一下内容:

1
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "vue.d.ts"],

Vscode开发vue+ts项目 报错:找不到模块或其相应的类型声明
https://xmas-nnnut.github.io/2023/06/25/Vscode开发ts项目-报错:找不到模块或其相应的类型声明/
作者
Xmas-nnnut
发布于
2023年6月25日
许可协议