【发布时间】:2018-07-15 07:18:22
【问题描述】:
我正在尝试让对象扩展运算符与我的反应项目一起使用。它出现了一个语法错误:
我已经尝试使用babel-plugin-transform-object-rest-spread,如 babel docs 所述。
经过一番研究,我还尝试了babel 的 GitHub 问题中描述的配置:
请在下面查看我的 .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
}
}
谁能帮我找出正确的配置来使用对象扩展运算符?
【问题讨论】:
-
你的代码是什么样子的?
-
...不是运算符,对象 spread/rest 不是 ES2015 的一部分,这就是为什么你需要一个额外的插件来转换它。
标签: reactjs webpack babeljs ecmascript-next