本文介绍了反应:ReferenceError:未定义regeneratorRuntime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的React应用程序中使用异步并等待.

I am trying to use async and await in my react application.

   onSubmit = async (model) => {
        await this.setState({ data: model });
    }

添加以上代码后,我的浏览器控制台出现错误.

After adding the above code i get an error in my browser console.

.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": [
        "@babel/plugin-proposal-class-properties"
    ],
    "sourceMaps": "inline"

}

webpack.config.js

const path = require("path");
const WebpackShellPlugin = require("webpack-shell-plugin");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = [
    {
    Server config removed

 },

    {
        entry: {
            app1: './src/public/app1/index.js',
            app2: './src/public/app2/index.js',
            app3: './src/public/app3/index.js',
        },
        devtool: "source-map",
        output: {
            path: __dirname + '/dist/public/',
            publicPath: '/',
            filename: '[name]/bundle.js',
            devtoolLineToLine: true,
            sourceMapFilename: "[name]/bundle.js.map",

        },
        module: {
            rules: [
                {
                    test: /(\.css|.scss)$/,
                    use: [{
                        loader: "style-loader" // creates style nodes from JS strings
                    }, {
                        loader: "css-loader" // translates CSS into CommonJS
                    }, {
                        loader: "sass-loader" // compiles Sass to CSS
                    }]
                },
                {
                    test: /\.(jsx|js)?$/,
                    use: [{
                        loader: "babel-loader",
                        // options: {
                        //     cacheDirectory: true,
                        //     presets: ['react', 'es2015'] // Transpiles JSX and ES6
                        // }
                    }]
                }
            ],

        },
        "plugins": [
            new CopyWebpackPlugin([
                {
                    from: 'src/public/app1/index.html',
                    to: 'app1'
                },
                {
                    from: 'src/public/app2/index.html',
                    to: 'app2'
                },
                {
                    from: 'src/public/app3/index.html',
                    to: 'app3'
                },
            ]),

        ]

    }
];

我添加了babelrc和webpack配置.如果我遗漏了一些会导致此错误显示在我的浏览器控制台中的内容,请告诉我.

I have added my babelrc and webpack config. Please let me know if i am missing something that would cause this error to appear in my browser console.

推荐答案

使用async/await在组件中导入regeneratorRuntime:

Import regeneratorRuntime in the component using async/await:

import regeneratorRuntime from "regenerator-runtime";

***更新后的***(可能在上面未使用)

*** UPDATED ANSWER *** (probably don't use above)

导入babel@babel/plugin-transform-runtime插件:

package.json

  "devDependencies": {
    "@babel/core": "^7.8.7",
    "@babel/plugin-transform-runtime": "^7.8.3",
  },

.babelrc

{
  "plugins": ["@babel/plugin-transform-runtime"]
}

这篇关于反应:ReferenceError:未定义regeneratorRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 20:17