个人技术分享

1. 找不到模块“@/views/HomeView.vue”或其相应的类型声明

今天帮同事看了一个问题,他尝试用vite+vue3+ts+pinia创建项目,结果刚上来就遇到这么一个问题

在这里插入图片描述

2. 解决办法

出现这个问题的原因就是:ts只支持导出导入模块,但是vue不是模块,我们需要申明一下vue是个模块,你ts可以导入

问题解决方法:在 typings 目录下有 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
}