【问题标题】:React async and await throwing error 'Uncaught ReferenceError: regeneratorRuntime is not defined'反应异步并等待抛出错误“未捕获的 ReferenceError:未定义 regeneratorRuntime”
【发布时间】:2019-06-10 23:56:35
【问题描述】:

我有这段代码

async makeRestCall() {
    var api = await PrognApi(this.state.Name);
    console.log(api);
  }

但反应是扔这个

未捕获的 ReferenceError: regeneratorRuntime 未定义

在控制台中。我知道我之前在 React 中使用过 async 和 await,但我不记得我认为我是如何安装库的。任何建议都会很棒。我正在使用 js 文件来保存我的 jsx。我也在使用 webpack 和 babel。

【问题讨论】:

  • 您是否尝试运行npm install @babel/runtime

标签: reactjs rest asynchronous async-await


【解决方案1】:

原来我需要 Babel-polyfill

【讨论】:

    【解决方案2】:

    我不确定“regeneratorRuntime”是什么,所以问题可能出在其他地方。但我看到了一些东西。

    首先,您的方法似乎缺少左括号:

    async makeRestCall() {
      var api = await PrognApi(this.state.Name);
      console.log(api);
    }
    

    其次,Name 的大写“N”是否正确?

    第三,我想知道你的方法是否有this 上下文?也许它无法访问您的组件的state。您可以尝试将其更改为箭头函数:

    makeRestCall = async () => {
      var api = await PrognApi(this.state.Name);
      console.log(api);
    };
    

    我觉得我没有足够的信息来解决此问题。也许更新您的问题以显示整个组件?

    祝你好运!

    【讨论】:

    • 我尝试使用箭头函数和 webpack 抱怨 ./components/ProgressionModal.js 中的 ERROR 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):SyntaxError: X:/ tconline/Services/TCAdminTool/TCAdminServer/Scripts/react/components/ProgressionModal.js:意外令牌 (56:15) 54 | 55 | > 56 | makeRestCall = async () => { | ^
    • 您可以在构造函数中绑定this 上下文:this.makeRestCall = this.makeRestCall.bind(this);。 (阅读更多here
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 2021-09-26
    • 1970-01-01
    • 2019-04-23
    • 2020-09-20
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多