likes
comments
collection
share

vue3+ts,在ts文件中导入vue文件,会报类似的错误:找不到模块“./App.vue”或其相应的类型声明

作者站长头像
站长
· 阅读数 47

vue3+ts,在ts文件中导入vue文件,会报类似的错误:找不到模块“./App.vue”或其相应的类型声明

问题:在.ts文件中报错,找不到模块“./XXX.vue”或其相应的类型声明报错原因:typescript只能理解.ts文件,无法理解.vue文件

解决方法:可以在 src 目录下创建一个 env.d.ts 文件(一般已存在),接着在里面增加下面的内容:

/// <reference types="vite/client" />
// 三斜线引用告诉编译器在编译过程中用types形式引入的额外的文件vite/client.d.ts,
// 此文件里面是vite帮我们定义的各种常用类型定义,比如css,图片等。

declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
  const component: DefineComponent<{}, {}, any>
  export default component
}

默认情况下,Vite 在 vite/client.d.ts 中为 import.meta.env 提供了类型定义。参考:https://vitejs.bootcss.com/gu...

在tsconfig.app.json中的include字段中加入"env.d.ts":

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