反应:ReferenceError:未定义regeneratorRuntime

时间:2018-11-26 08:49:44

标签: reactjs webpack babeljs

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

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

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

  

ReferenceError:未定义regeneratorRuntime

.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配置。如果我遗漏了一些会导致此错误显示在浏览器控制台中的内容,请告诉我。

3 个答案:

答案 0 :(得分:6)

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

import regeneratorRuntime from "regenerator-runtime";

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

导入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"]
}

答案 1 :(得分:0)

您没有包含package.json文件,因此不清楚您丢失了什么。

假设您的package.json文件中有@babel/polyfill作为依赖项,是否有可能未指定:

import '@babel/polyfill'

在您的React文件中(例如index.js)?

答案 2 :(得分:0)

yarn add --dev react-app-polyfill 添加polyfill对我有用。

webpack.config.js

然后将以下行添加到entry: { app: [ 'react-app-polyfill/ie9', // Only if you want to support IE 9 'react-app-polyfill/stable', './src/index.jsx', ], },

@Secured(['IS_AUTHENTICATED_FULLY'])

react-app-polyfill GitHub page上查看更多示例。