vue3加ts出现 --找不到模块“@/views/HomeView.vue”或其相应的类型声明。

在使用Vite、Vue3和TypeScript构建项目时遇到了模块导入问题,错误提示找不到'@/views/HomeView.vue'。问题在于TS不识别Vue组件。解决方案是在项目的env.d.ts文件中声明Vue组件为模块。若文件不存在,则需新建。在env.d.ts中添加声明,允许TS导入Vue组件。此方法帮助开发者解决Vue3和TS结合时的导入冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue3加ts出现 --找不到模块“@/views/HomeView.vue”或其相应的类型声明。

今天在尝试用vite+vue3+ts+pinia创建项目,结果刚上来就遇到这么一个问题,
在这里插入图片描述
屎壳郎它不咬人,他恶心人呀😒,身为强迫症的我受不了!!!
出现这个问题的原因大概就是:ts只支持导出导入模块,但是vue不是模块,我们需要申明一下vue是个模块,你ts可以导入,
问题解决方法:

  • 目录下有env.d.ts文件
在文件中加上
declare module '*.vue' {
   import type { DefineComponent } from 'vue'
   const component: DefineComponent<{}, {}, any>
   export default component
}
或者
declare module '*.vue' {
   import type { DefineComponent } from 'vue'
   const component: ComponentOptions | ComponentOptions['setup']
   export default component
}
  • 目录下没有就新建一个喽。
    有用麻烦点个小心心哦,拜了个拜。
(!) Failed to run dependency scan. Skipping dependency pre-bundling. Error: The following dependencies are imported but could not be resolved: @/views/HomeView.vue (imported by D:/Vuezy/learn-plan-system/src/router/index.js) @/views/AddTaskView.vue (imported by D:/Vuezy/learn-plan-system/src/router/index.js) Are they installed? at file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:11238:33 at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:34671:15 23:35:49 [vite] (client) Pre-transform error: Failed to resolve import "@/views/HomeView.vue" from "src/router/index.js". Does the file exist? Plugin: vite:import-analysis File: D:/Vuezy/learn-plan-system/src/router/index.js:2:22 1 | import { createRouter, createWebHistory } from &#39;vue-router&#39; 2 | import HomeView from &#39;@/views/HomeView.vue&#39; | ^ 3 | import AddTaskView from &#39;@/views/AddTaskView.vue&#39; 4 | 23:35:49 [vite] Internal server error: Failed to resolve import "@/views/HomeView.vue" from "src/router/index.js". Does the file exist? Plugin: vite:import-analysis File: D:/Vuezy/learn-plan-system/src/router/index.js:2:22 1 | import { createRouter, createWebHistory } from &#39;vue-router&#39; 2 | import HomeView from &#39;@/views/HomeView.vue&#39; | ^ 3 | import AddTaskView from &#39;@/views/AddTaskView.vue&#39; 4 | at TransformPluginContext._formatLog (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:31446:43) at TransformPluginContext.error (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:31443:14) at normalizeUrl (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:29992:18) at async file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:30050:32 at async Promise.all (index 1) at async TransformPluginContext.transform (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:30018:4) at async EnvironmentPluginContainer.transform (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:31260:14) at async loadAndTransform (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:26434:26) at async viteTransformMiddleware (file:///D:/Vuezy/learn-plan-system/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:27519:20) 23:35:49 [vite] (client) ✨ new dependencies optimized: vue, pinia, vue-router
06-27
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值