我的组件构造函数中有这段代码:
this.state = {
captcha: {...ABC.XYZ}
};
我尝试了不同的变体,比如
const abc = ABC.XYZ;
const dummy = {...abc};
我总是得到错误。
Unexpected token (36:18) Use --force to continue
但是,在我的 JSX 中,我有
<Login
{...props}
/>
这工作正常。
如果是 Babel 的问题,不应该到处报错吗?
一种有效,一种无效,因为它们是两个独立的功能。
<Login
{...props}
/>
是一个JSX 传播属性,而
this.state = {
captcha: {...ABC.XYZ}
};
是一个ES2018 对象 spread。
由于第一个是 JSX 的一个特性,所以它在处理 JSX 时被 Babel 转化。第二,您需要在 Babel 中显式启用https://babeljs.io/docs/plugins/transform-object-rest-spread/。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句