【发布时间】:2018-04-14 22:50:09
【问题描述】:
我正在学习在我的一个反应项目中使用 es6 并应用新的扩展运算符 ... 这在使用数组 [](作为扩展运算符)语法时工作正常,但在使用时失败内部对象{}(作为其余属性)即不分配值只是修改。
这两者是完全不同的东西吗?这是我的系统详细信息
- 节点 v 6.11.4
- babel-core v 6.26.0
- macOS Sierra v 10.12.6
- Sublime Text 3 build 3143
/*eslint no-unused-vars:0 */
let alpha = ['a','b','c', {first: 'first'}];
let beta = ['be', 'ta', { first: 'second'}];
let more = {text:'more', date: new Date()};
const gamma = [...alpha, more]; // this is working fine
console.log(gamma, alpha);
let todos = [{id: 1, completed: false}, {id:2, completed: true}];
const noCurlyFatArrow = () => {
return todos.map(todo =>
(todo.id === action.index)
? {...todo, completed: !todo.completed }
: todo
)
};
noCurlyFatArrow();
并在 sublime 文本 (⌘ + B) 中运行 JS 构建系统,它会在控制台中出现以下错误
/opt/rqt/src/react-only/spread.js:1
? {...todo, completed: !todo.completed }
^^^
SyntaxError: Unexpected token ...
还从 issue dicussion 对 .babelrc filr 进行了一些更改,但没有运气。
.babelrc
{
"presets": ["es2015", "stage-3", "react"], //
"plugins": ["transform-es2015-destructuring", "transform-object-rest-spread"]
}
尝试不使用“舞台”,也尝试使用"stage-0"。
package.json
{
"name": "rqt",
"version": "0.1.0",
"private": true,
"dependencies": {
"jquery": "^3.2.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-redux": "^5.0.6",
"react-scripts": "1.0.14",
"redux": "^3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-eslint": "^7.2.3",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"eslint": "^4.10.0",
"eslint-config-airbnb": "^16.0.0",
"eslint-config-react-app": "^2.0.1",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-html": "^4.0.0-alpha.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.4.0"
}
}
【问题讨论】:
-
在 object literals 中使用 rest/spread 不是有效的 ES6 语法
-
但反应家伙正在使用example
-
但是 react 的人使用的是他们自己的 babel 预设,而不是 ES6
-
@Bergi 他们正在使用 transform-object-rest-spread 插件,我认为这不是问题所在。
-
我已经安装了
transform-object-rest-spread插件并添加到了.babelrc,但是它不起作用。
标签: ecmascript-6 babeljs