解决报错:
1、安装:
pnpm add @types/node -D
2、配置别名
vite.config.ts:
import path from 'path'
resolve: {
//设置路径别名
alias: {
'@': path.resolve(process.cwd(), './src')
}
},
2、src下新增vite-env.d.ts文件
/// <reference types="vite/client" />
// 配置这个文件是 解决错误:找不到模块“@/views/login/index.vue”或其相应的类型声明。ts(2307)
// 这段代码告诉 TypeScript,所有以 .vue 结尾的文件都是 Vue 组件,可以通过 import 语句进行导入。这样做通常可以解决无法识别模块的问题。
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<typeof defineComponent>
export default component
}
完成后一定要在tsconfig.json中添加在include中
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": false,
"sourceMap": true,
"baseUrl": "src",
"outDir": "dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"declaration": true,
"strict": false,
"types": ["vite/client"],
"paths": {
"@/*": ["./*"],
"@": ["./"]
}
},
"include": [
"src/**/*.ts",
"electron/**/*.ts",
"types/*",
"vite-env.d.ts",
"src/*",
"src/**/*.vue"
]
}
最重点的就是一定要把这个加上, "src/**/*.vue",和 "paths": {"@/*": ["./*"],"@": ["./"]}。
然后重启一下编辑器就可以了。