一、现象与原因
vite.config.ts可以配置resolve.alias别名,这样在项目中import时就不用写一长串相对路径了,但有时会遇到编辑器提示『找不到模块』,虽然不影响编译与运行,但看着很碍眼。
原因就是缺少了相应的配置,导致VSCode识别不了模块。

二、针对.vue文件
在项目的src目录下新建vite-env.d.ts,写入以下内容:
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
三、针对.ts文件
在项目根目录下的tsconfig.json文件中加入baseUrl和path两个选项,具体的值就根据你的实际情况修改:
{
"compilerOptions": {
//...
"baseUrl": "src",
"paths": {
"@hooks/*": ["hooks/*"]
}
}
//...
}
当在Vite项目中使用resolve.alias配置别名时,可能会遇到VSCode编辑器显示找不到模块的提示。这可以通过创建vite-env.d.ts文件并声明.vue文件类型,以及在tsconfig.json中设置baseUrl和path来解决。在vite-env.d.ts中,导入vite/client并声明.vue组件定义;在tsconfig.json中,添加baseUrl和针对别名的路径配置,使编辑器能正确识别模块路径。
4921

被折叠的 条评论
为什么被折叠?



