gpt4 book ai didi

reactjs - 使对象传播以使用 webpack.config 和 babelrc 文件

转载 作者:行者123 更新时间:2023-12-04 03:01:42 26 4
gpt4 key购买 nike

我正在尝试让对象传播运算符与我的 React 项目一起工作。它出现语法错误:

spread operator error

我已经尝试使用 babel-plugin-transform-object-rest-spread,如 babel docs 所述.

经过一番研究后,我还尝试了 GitHub 问题中描述的配置 babel : babelrc config for spread operator

请查看下面我的 .babelrc 文件:

{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread"],
"sourceMaps": true,
"retainLines": true
}

下面是我的webpack.config 文件:

const path = require('path');

module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public', 'js'),
publicPath: '/'
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.jsx?$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel-loader',
options: {
presets: ['react', 'es2015']
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test:/\.scss$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
}, {
loader: 'sass-loader', options: {
sourceMap: true
}
}]
},
{
test: /.(png|jpg|jpeg|gif|svg|woff|woff2|eot|ttf)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader'
}
]
},
devServer: {
historyApiFallback: true,
contentBase: path.join(__dirname, 'public'),
publicPath: '/js/',
port: 3000
}
};

我使用展开运算符的代码是:

import * as types from '../types/types'; 

const initialState ={
mesage:''
}

export default function doodlesReducer (state = initialState, action) {
switch(action.type) {
case 'SET_MESSAGE' :
return {…state, message: action.payload.message}
default :
return state
}
}

谁能帮我找出使用对象展开运算符的正确配置?

最佳答案

你在 .babelrcwebpack.config 中列出了你的预设,尝试将它全部移到其中一个,即只有 .babelrc :

{
"presets": ["es2015", "react"],
"plugins": ["transform-object-rest-spread"]
}

编辑:另外,不要使用现已弃用的 es2015 预设,而是安装 env 预设 npm install --save- dev babel-preset-env 并在 .babelrc 中将 es2015 替换为 env

编辑 2: 您正在使用的 ... 使用了一些不受支持的字符编码,您必须从某个地方复制粘贴它,从而弄乱了编码,替换它与 ...

关于reactjs - 使对象传播以使用 webpack.config 和 babelrc 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48622489/

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