简体   繁体   English

如何修复反应代码中的vscode自动间距问题

[英]How to fix vscode auto spacing issues in react code

Im trying to save some data into a state using object keys in react,我正在尝试使用 object 键将一些数据保存到 state 中,

Ex: setText(data[listname][0].feature-type;例如:setText(data[listname][0].feature-type;

But after saving the file it automatically adding a space between featute and type但保存文件后,它会自动在功能和类型之间添加一个空格

Ex: setText(data[listname][0].feature - type:例如:setText(data[listname][0].feature - 类型:

As it is this will resulted as reference error.How to avoid this因为这将导致参考错误。如何避免这种情况

As commented, this is the solution if feature-type is a property of the object you are trying to access:正如所评论的,如果 feature-type 是您尝试访问的 object 的属性,则这是解决方案:

You need to access it as a dictionary setText(data[listname][0]['feature-type'] .您需要以字典setText(data[listname][0]['feature-type']访问它。

Because there are some limitations to which names can be accessed with a dot (.).因为可以使用点 (.) 访问名称存在一些限制。 And since the property name contains hífen (-), it needs to be accessed as a dictionary.由于属性名称包含 hífen (-),因此需要将其作为字典进行访问。 VSCode is formatting different then what you expect because you are giving it invalid syntax. VSCode 的格式与您期望的不同,因为您给它的语法无效。

You can look at this answer: https://stackoverflow.com/a/29888297/8633918 for more details.您可以查看此答案: https://stackoverflow.com/a/29888297/8633918了解更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM