简体   繁体   English

使用VS Code的React文件中的清理格式

[英]Cleanup format in React file with VS Code

So I am curious how I can quickly correct the formatting of my code in a .jsx file. 因此,我很好奇如何快速纠正.jsx文件中代码的格式。 Have tried some extensions like beautify react, prettify, etc...., but they do not seem to work quite right in vs code. 尝试了一些扩展,例如美化反应,美化等等...,但是它们似乎在vs代码中不能正常工作。

What I see is this: 我看到的是:

在此处输入图片说明

You can see where my indent lines are messed up. 您可以看到我的缩进线被弄乱了。 I believe this to be from developers on team using other editors with their spacing set differently. 我相信这是来自团队中使用其他编辑器的开发人员,其间距设置不同。

So is there a way to keep this clean and maybe add some automagic to this? 那么,有没有一种方法可以保持这种状态清洁,甚至可以为其添加一些自动功能?

The one which goes perfectly well for my .jsx and .js is Prettier 肚里非常清楚我的一个.jsx.js更漂亮

You can install it as a project dependency: 您可以将其安装为项目依赖项:

npm install --save-dev --save-exact prettier

If you want prettier to watch for changes , you can add following to your package.json 如果您想更漂亮地监视更改,可以将以下内容添加到package.json

  "scripts": {
    "prettier-watch": "onchange '**/*.js' -- prettier --write {{changed}}"
  }

VS prettier Configuration VS更漂亮的配置

You can see other Prettier editor configuration for more details 您可以查看其他Prettier编辑器配置以了解更多详细信息

eslint has the ability to auto-fix files via a --fix command-line argument if you have style rules defined. eslint有通过为自动修复文件的能力--fix如果你有定义的样式规则的命令行参数。 It has a fairly substantial set of rules available via plugins. 它具有可通过插件使用的相当大量的规则。 There are also standardized configs like https://github.com/standard/eslint-config-standard-react . 还有标准化的配置,例如https://github.com/standard/eslint-config-standard-react This will help you out with formatting and enforcing other best practices. 这将帮助您格式化和执行其他最佳实践。

It integrates with vscode . 它与vscode集成

Visual Studio 2017 has decent formatting options for .jsx files. Visual Studio 2017为.jsx文件提供了不错的格式设置选项。 What makes formatting them difficult is that they combine markup with JavaScript. 使格式化变得困难的原因是它们将标记与JavaScript结合在一起。 I'm not sure if VS Code has this facility, but you can use this keyboard shortcut to cleanup the formatting of .jsx files. 我不确定VS Code是否具有此功能,但是您可以使用此键盘快捷键来清理.jsx文件的格式。

Ctrl + K + D Ctrl + K + D

It also will cleanup the formatting of your json etc. I think it's the first version of Visual Studio that offers this level of formatting for .jsx. 它还将清除json等的格式。我认为这是Visual Studio的第一个版本,它为.jsx提供这种格式的格式。

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

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