From 3f20e034d2a2c5a39de04525969d769237525c23 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Mon, 18 Nov 2019 22:14:48 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E7=BC=96=E7=A8=8B=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/async-await.md | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/async-await.md b/docs/async-await.md index 35e29fb50858..62ec1140311a 100644 --- a/docs/async-await.md +++ b/docs/async-await.md @@ -2,19 +2,40 @@ title: 异步编程 --- -Taro 支持使用 `async functions` 来让开发者获得不错的异步编程体验,开启 `async functions` 支持需要安装包 `@tarojs/async-await` +Taro 支持使用 `async functions` 来让开发者获得不错的异步编程体验,开启 `async functions` 支持需要使用到 `babel-plugin-transform-runtime` 插件,请按如下操作 + +首先需要在项目中分别安装 `babel-plugin-transform-runtime` 和 `babel-runtime` 两个包 ```bash -$ yarn add @tarojs/async-await -# 或者使用 npm -$ npm install --save @tarojs/async-await +$ yarn add babel-plugin-transform-runtime --dev +$ yarn add babel-runtime ``` -随后在项目入口文件 `app.js` 中直接 `import` ,就可以开始使用 `async functions` 功能了 +随后修改项目 [`babel` 配置](./config-detail.md#babel),增加插件 `babel-plugin-transform-runtime` -```javascript -// src/app.js -import '@tarojs/async-await' +```js +babel: { + sourceMap: true, + presets: [ + [ + 'env', + { + modules: false + } + ] + ], + plugins: [ + 'transform-decorators-legacy', + 'transform-class-properties', + 'transform-object-rest-spread', + ['transform-runtime', { + "helpers": false, + "polyfill": false, + "regenerator": true, + "moduleName": 'babel-runtime' + }] + ] +} ``` -> 值得注意的事,使用 `@tarojs/async-await` 一定要记得按照[开发前注意](./before-dev-remind.md)中提示的内容进行操作,否则会出现报错 +这样就能直接在项目中使用 `async functions` 了。