vue3项目加入ts,解决报错:找不到模块“@/components/EnjoyPackage/Swipers.vue”或其相应的类型声明。ts-plugin(2307)

解决报错: 

 

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": {"@/*": ["./*"],"@": ["./"]}。

然后重启一下编辑器就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值