重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Taro新建项目使用async报错regeneratorRuntimeisnotdefined

当用 Taro 命令 taro init myApp 创建项目的时候,等程序运行结束,进入到 myApp 目录中,执行 npm run dev:weapp 的时候,代码可以正常运行。

成都创新互联服务项目包括米易网站建设、米易网站制作、米易网页制作以及米易网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,米易网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到米易省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

但是当我们在代码中用 async/await 的方式写了一些代码之后,就不能正常运行了,报错如下:

VM49:1 thirdScriptError
regeneratorRuntime is not defined
ReferenceError: regeneratorRuntime is not defined

原因就是缺少安装 babel-plugin-transform-runtime 和 babel-runtime 两个依赖。

yarn add babel-plugin-transform-runtime --dev
yarn add babel-runtime

⚠️注意:如果是 Taro v1 版本的话,还需要安装 @tarojs/async-await 这个包。升级到 Taro v2 版本后就不需要这个包了。

安装完成之后,需要修改一下 config/index.js 文件:

plugins: [
    'transform-decorators-legacy',
    'transform-class-properties',
    'transform-object-rest-spread',
+   ['transform-runtime', {
+       "helpers": false,
+       "polyfill": false,
+       "regenerator": true,
+       "moduleName": 'babel-runtime'
+   }]
}

其中前面带有 + 部分是需要添加的内容。

另外,当我们在 Taro中使用 async 的方式写代码时,还需要在开发前配置这些地方请添加链接描述。

再次运行,小程序就不会报错啦!

我的博客:https://www.wanghuiblog.com


网站题目:Taro新建项目使用async报错regeneratorRuntimeisnotdefined
本文地址:http://cqcxhl.cn/article/gsgesp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP