uni-app中如何使用ES11 matchAll 新特性进行编译
uni-app中如何使用ES11 matchAll 新特性进行编译
操作步骤:
- 使用ES11新特性matchAll处理字符串
预期结果:
- babel编译为ES5后全平台运行
实际结果:
- 未编译导致QQ小程序报错
bug描述:
- 同属于ES11新特性的可选链和matchAll后者没有编译,导致在QQ小程序上报错
相关图片

| 信息类别 | 内容 |
|---|---|
| 产品分类 | uniapp/小程序/QQ |
| PC开发环境 | Mac |
| PC开发环境版本 | 10.15.6 |
| HBuilderX类型 | 正式 |
| HBuilderX版本 | 3.1.12 |
| 工具版本号 | 3.1.4.20210305 |
| 基础库版本 | 3.260.37 |
| 项目创建方式 | HBuilderX |
更多关于uni-app中如何使用ES11 matchAll 新特性进行编译的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
支持度不好,只有高版本的手机系统才能支持
更多关于uni-app中如何使用ES11 matchAll 新特性进行编译的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在 uni-app 中,ES11 的 matchAll 方法默认可能未被 Babel 转译,导致在 QQ 小程序等不支持该特性的环境中报错。这是因为 matchAll 属于较新的 ES 特性,而 Babel 配置可能未包含对应的 polyfill 或插件。
解决方案:
-
检查 Babel 配置:确认
babel.config.js中已包含[@babel](/user/babel)/plugin-proposal-string-matchall插件。若未安装,执行:npm install --save-dev [@babel](/user/babel)/plugin-proposal-string-matchall并在配置中添加:
module.exports = { plugins: ['[@babel](/user/babel)/plugin-proposal-string-matchall'] }; -
使用 Core-JS Polyfill:在项目入口文件(如
main.js)中引入 Core-JS 的对应模块:import 'core-js/features/string/match-all';确保已安装 Core-JS:
npm install core-js -
降级代码写法:若编译仍不生效,可暂时用
RegExp的exec方法替代matchAll,例如:// 原代码:const matches = str.matchAll(/pattern/g); const matches = []; let match; while ((match = /pattern/g.exec(str)) !== null) { matches.push(match); }

