简体   繁体   English

反应:ReferenceError:regeneratorRuntime 未定义

[英]React: ReferenceError: regeneratorRuntime is not defined

I am trying to use async and await in my react application.我正在尝试在我的 React 应用程序中使用 async 和 await。

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

After adding the above code i get an error in my browser console.添加上述代码后,我的浏览器控制台出现错误。

ReferenceError: regeneratorRuntime is not defined参考错误:未定义 regeneratorRuntime

.babelrc .babelrc

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

}

webpack.config.js 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'
                },
            ]),

        ]

    }
];

I have added my babelrc and webpack config.我已经添加了我的 babelrc 和 webpack 配置。 Please let me know if i am missing something that would cause this error to appear in my browser console.如果我遗漏了会导致此错误出现在我的浏览器控制台中的内容,请告诉我。

Import regeneratorRuntime in the component using async/await:使用 async/await 在组件中导入 regeneratorRuntime:

import regeneratorRuntime from "regenerator-runtime";

*** UPDATED ANSWER *** (probably don't use above) *** 更新的答案 ***(可能不要在上面使用)

Import babel and @babel/plugin-transform-runtime plugin:导入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"]
}

You did not include your package.json file, so it is a bit unclear what you are missing.你没有包含你的 package.json 文件,所以有点不清楚你缺少什么。

Assuming you have @babel/polyfill as a dependency in your package.json file, is it possible that you are not specifying:假设您在 package.json 文件中有@babel/polyfill作为依赖项,您是否可能没有指定:

import '@babel/polyfill'

in your React file (such as index.js)?在你的 React 文件中(比如 index.js)?

Adding polyfills from create-react-app worked for me.create-react-app添加 polyfills 对我有用。

yarn add --dev react-app-polyfill

Then add the following lines to webpack.config.js然后webpack.config.js添加到webpack.config.js

entry: {
  app: [
    'react-app-polyfill/ie9', // Only if you want to support IE 9
    'react-app-polyfill/stable',
    './src/index.jsx',
  ],
},

See more examples on the react-app-polyfill GitHub page .react-app-polyfill GitHub 页面上查看更多示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 ReferenceError:RegeneratorRuntime 未在 React 中定义 - Uncaught ReferenceError: regeneratorRuntime is not defined in React 未捕获的 ReferenceError:未定义 regeneratorRuntime - Uncaught ReferenceError: regeneratorRuntime is not defined 反应异步并等待引发错误“未捕获的ReferenceError:未定义regeneratorRuntime” - React async and await throwing error 'Uncaught ReferenceError: regeneratorRuntime is not defined' 无服务器 create-react-app - ReferenceError: regeneratorRuntime 未定义 - serverless create-react-app - ReferenceError: regeneratorRuntime is not defined Rete 库给 React JS 带来错误:ReferenceError: regeneratorRuntime is not defined - Rete Library giving error with React JS : ReferenceError: regeneratorRuntime is not defined Babel 7 - 未捕获的 ReferenceError:未定义 regeneratorRuntime - Babel 7 - Uncaught ReferenceError: regeneratorRuntime is not defined 使用 react-speech-recognition 时出现反应错误“ReferenceError: regeneratorRuntime is not defined” - React Error when using react-speech-recognition “ReferenceError: regeneratorRuntime is not defined” ReferenceError:运行测试时未定义 regeneratorRuntime - ReferenceError: regeneratorRuntime is not defined when running testing 未捕获的 ReferenceError:regeneratorRuntime 未在反应 17、webpack 5 中定义,同时通过操作进行 api 调用 - Uncaught ReferenceError: regeneratorRuntime is not defined in react 17, webpack 5 while making api calls through actions React Webpack 不渲染 DOM(未定义 regeneratorRuntime) - React Webpack not render DOM (regeneratorRuntime is not defined)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM