Taro new project using async error regeneratorRuntime is not defined

When taro init myApp create a project with Taro command when running programs such as then entered the myApp directory, execute npm run dev: weapp when the code can be run properly.

But when we wrote some code in the code with async mode / await, you can not run properly, being given as follows:

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

Reason is the lack of installation babel-plugin-transform-runtime and two babel-runtime dependency.

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

⚠️ Note: If Taro v1 version, also need to install @tarojs/async-awaitthis package. Taro v2 upgrade to version after not need this package.

After the installation is complete, you need to modify config / index.js file:

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

Wherein the moiety is preceded by a + content to be added.

In addition, when we use async in Taro way to write code, you also need to configure these places add a linker script before development .

Run again, the applet would not have given it!

My blog: https://www.wanghuiblog.com

Guess you like

Origin blog.51cto.com/6638574/2464092