我正在构建一个小型 React 组件(使用 Babel 编译)。我必须访问名称中带有破折号的属性。
this.setState({
newArtist: {
birthdate: artist.life-span.begin
}
});
此代码抛出错误Uncaught ReferenceError: span is not defined
经过一些研究,我发现我需要使用括号来替代表示法。 ['生命周期']
this.setState({
newArtist: {
birthdate: artist['life-span']begin
}
});
但是这个让 Babel 抛出一个语法错误。
bundle.js:1 SyntaxError:/file/path: Unexpected token, expected , (24:38) while parsing file
所以我被困在这里了。
我的 babel 配置很简单,只使用了 es2015 和 react 预设。
知道它可能是什么吗?
最佳答案
birthdate: artist['life-span'].begin
而不是生日:艺术家['life-span']开始
关于javascript - 无法在 React Js(Babel) 中使用连字符 “-” 访问 JSON 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40678800/