【问题标题】:Need help to make [create-react-app] to use Aync - Await (transform-async-to-generator)!需要帮助以使 [create-react-app] 使用 Async - Await (transform-async-to-generator)!
【发布时间】:2018-03-20 07:40:26
【问题描述】:

我是 [create-react-app] 的新手,我想了解如何将 ["transform-async-to-generator"] 添加到此构建过程中?在常规情况下,我会将它添加到 .babelrc 中,但看起来不适用于 [create-react-app]。

*通过“看起来不起作用” - 我看到以下错误。

Syntax error: ../web/src/App.js: Unexpected token, expected ( (17:13)

  15 |   }
  16 |
> 17 |   test = async () => {
     |              ^
  18 |     let x = await this.resolveAfter2Seconds();
  19 |     try{}
  20 |     catch(exception){

还有什么方法可以扩展 [create-react-app] 而不修改包本身?

谢谢!

【问题讨论】:

    标签: reactjs webpack babeljs create-react-app


    【解决方案1】:

    问题不在于异步函数。你应该用下一个方法重写你的代码:

    // ...
    
    test = async () => {
      let x = await this.resolveAfter2Seconds();
      // ...
    }
    

    // ...
    async test(){
      let x = await this.resolveAfter2Seconds();
      // ...
    }
    

    【讨论】:

    • 哦!抱歉处理错误的代码。我错过了:@imcvampire 提到的 babel-plugin-transform-async-to-generator 和 babel-plugin-syntax-async-functions。
    【解决方案2】:

    你应该给 babel 添加 2 个插件:babel-plugin-transform-async-to-generatorbabel-plugin-syntax-async-functions

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 2018-11-27
      • 2017-07-04
      • 1970-01-01
      • 2020-02-11
      • 2017-09-11
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多