VUE+TS 引入文件报错 找不到对应模块
报错:Cannot find module ‘@/views/login.vue’ or its corresponding type declarations.ts(2307)
原因:ts文件不识别以@符号引用的路径
解决方案:
找到vite-env.d.ts文件,添加以下代码
declare module '*.vue' {
import { DefineComponent } from "vue"
const component: DefineComponent<{}, {}, any>
export default component
}
一般情况就好了,如果还不好,重启一下编辑器就可以了!
还有一种情况:
tsconfig.json中没有配置别名配置
{
"compilerOptions": {
// @ 别名配置 路径提示
"baseUrl": ".",
"paths": {
"@/*":["/src/*"],
}
}
}
到此为止就可以了
使用@引用文件在vite.config.ts中添加一下配置即可
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
}
}
})
}
谨此记录一下