Vue: Cannot find module ../views/HelloWorld.vue or its corresponding type declarations.


这个错误通常出现在Vue 3 + TypeScript项目中,主要原因是TypeScript无法识别.vue文件类型。以下是几种解决方案:

    1. 1‌、Vetur替换为Volar‌:
      如果是VSCode用户,需要将Vetur扩展替换为Volar扩展,因为Vetur是为Vue 2设计的。

    2. 2‌、添加类型声明‌:
      在项目根目录的env.d.ts文件中添加以下声明:

    3. declare module '*.vue' {
        import { DefineComponent } from 'vue'
        const component: DefineComponent<{}, {}, any>
        export default component
      }
      
    4. 如果没有这个文件,需要新建一个。

      1. 3‌、检查tsconfig配置‌:
        确保tsconfig.json中包含了对.vue文件的识别:
      2. "include": [
          "src/**/*.ts",
          "src/**/*.tsx", 
          "src/**/*.vue",
          "env.d.ts"
        ]
        
    5. 4、查别名配置‌:
      如果是vite项目,确保vite.config.ts中配置了正确的路径别名。

    6. 5‌、模块解析策略‌:
      在tsconfig.json中设置"moduleResolution": "node"。

这些解决方案中,添加类型声明和配置Volar是最关键的步骤。

发布时间 : 2025-08-22,阅读量:3
本文链接:https://upwqy.com/details/987.html
vue3 中使用nprogress vue项目中引入pinia