Skip to content

Commit

Permalink
docs: 更新异步编程文档
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent cd264dd commit 2a4bd98
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions docs/async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 了。

0 comments on commit 2a4bd98

Please sign in to comment.