uni-app vue3+ts编译到微信小程序时类型声明缺失

uni-app vue3+ts编译到微信小程序时类型声明缺失

示例代码:

import { defineComponent, ref, onMounted, getCurrentInstance } from 'vue'

操作步骤:

  1. cli创建ts项目模版
  2. 使用vue3的API进行开发编译

预期结果:

编译正常无报错

实际结果:

常用api在vue上无法找到

bug描述:

cli创建项目编译到微信小程序时报错 Module ‘“vue”’ has no exported member ‘ref’. Module ‘“vue”’ has no exported member ‘onMounted’. Module ‘“vue”’ has no exported member ‘getCurrentInstance’.


| 信息类型         | 信息内容           |
|------------------|--------------------|
| 产品分类         | uniapp/小程序/微信 |
| PC开发环境操作系统 | Mac                |
| PC开发环境操作系统版本号 | mac os big sur 11.6 |
| 项目创建方式     | CLI                |
| CLI版本号        | 4.5                |

更多关于uni-app vue3+ts编译到微信小程序时类型声明缺失的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

typescript 版本不兼容问题。升级 typescript 版本即可,未检查测试最低兼容版本,升级到 typescript@4.4.4 可解决问题。

更多关于uni-app vue3+ts编译到微信小程序时类型声明缺失的实战教程也可以访问 https://www.itying.com/category-93-b0.html


确实,我也无意中升到最新的版本,确实ok啦

拜谢~升级到最新版本,解决了~

遇到同样的问题,请问有怎么有怎么解决吗?

怎么解决的啊

遇到同样的问题,没有解决

把typescript升级到最新版本即可~

楼主的方式我没试过,我是把 vue3.0.0 升级到 vue3.2.0 版本,就没有声明报错了

这个问题通常是由于 Vue 3 类型声明配置不正确导致的。以下是解决方案:

  1. 检查 tsconfig.json 配置: 确保在 tsconfig.json 中正确配置了 Vue 3 的类型声明:
{
  "compilerOptions": {
    "types": ["@dcloudio/types"],
    "lib": ["ES2015"],
    "module": "ESNext",
    "target": "ES2015"
  }
}
  1. 确认依赖版本: 检查 package.json 中相关依赖版本:
{
  "dependencies": {
    "@vue/shared": "^3.0.0",
    "vue": "^3.0.0"
  },
  "devDependencies": {
    "@dcloudio/types": "^3.0.0",
    "@vue/compiler-sfc": "^3.0.0"
  }
}
  1. 清理并重新安装
# 删除 node_modules 和 package-lock.json
rm -rf node_modules package-lock.json

# 重新安装依赖
npm install

# 清理构建缓存
npm run dev:mp-weixin -- --clean
  1. 检查 IDE 配置: 如果使用 VSCode,可以:
  • 重启 TypeScript 服务(Cmd+Shift+P → “TypeScript: Restart TS server”)
  • 确保 VSCode 使用的是项目本地的 TypeScript 版本
  1. 创建声明文件: 在项目根目录创建 vue-shim.d.ts
declare module '*.vue' {
  import { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}
回到顶部