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

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

VM49:1 thirdScriptErrorregeneratorRuntime is not definedReferenceError: regeneratorRuntime is not defined

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

yarn add babel-plugin-transform-runtime --devyarn 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