Kay*_*Kay 8 reactjs webpack babeljs
我正在尝试在我的 React 应用程序中使用 async 和 await。
onSubmit = async (model) => {
await this.setState({ data: model });
}
Run Code Online (Sandbox Code Playgroud)
添加上述代码后,我的浏览器控制台出现错误。
参考错误:未定义 regeneratorRuntime
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-proposal-class-properties"
],
"sourceMaps": "inline"
}
Run Code Online (Sandbox Code Playgroud)
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'
},
]),
]
}
];
Run Code Online (Sandbox Code Playgroud)
我已经添加了我的 babelrc 和 webpack 配置。如果我遗漏了会导致此错误出现在我的浏览器控制台中的内容,请告诉我。
Ste*_*Bob 15
使用 async/await 在组件中导入 regeneratorRuntime:
import regeneratorRuntime from "regenerator-runtime";
Run Code Online (Sandbox Code Playgroud)
*** 更新的答案 ***(可能不要在上面使用)
导入babel
和@babel/plugin-transform-runtime
插件:
package.json
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/plugin-transform-runtime": "^7.8.3",
},
Run Code Online (Sandbox Code Playgroud)
.babelrc
{
"plugins": ["@babel/plugin-transform-runtime"]
}
Run Code Online (Sandbox Code Playgroud)
您没有包含 package.json 文件,因此有点不清楚您缺少什么。
假设@babel/polyfill
您的 package.json 文件中有依赖项,您是否可能没有指定:
import '@babel/polyfill'
在你的 React 文件中(例如 index.js)?