gpt4 book ai didi

reactjs - react : ReferenceError: regeneratorRuntime is not defined

转载 作者:行者123 更新时间:2023-12-03 13:45:01 25 4
gpt4 key购买 nike

我正在尝试在我的 React 应用程序中使用 async 和 await。

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

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

ReferenceError: regeneratorRuntime is not defined



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

最佳答案

使用 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"]
}

关于reactjs - react : ReferenceError: regeneratorRuntime is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53477466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com