【问题标题】:Format *.tsx files on save in VS Code在 VS Code 中保存时格式化 *.tsx 文件
【发布时间】:2019-08-27 11:40:35
【问题描述】:

settings.json 中,只能格式化*.ts 文件:

"[typescript]": {
    "editor.formatOnSave": true
}

但我无法让它为 *.tsx 文件工作。

【问题讨论】:

    标签: typescript visual-studio-code react-tsx


    【解决方案1】:
    "[typescriptreact]": {
        "editor.formatOnSave": true
    }
    

    另见Language specific editor settings

    【讨论】:

    • 自 2020 年 10 月 15 日起这不起作用,您知道发生了什么吗?
    • 嗯,我刚刚检查了一下,它似乎对我有用。你错过了更新吗?你在其他地方把它设置为假吗?您是否禁用了格式化程序?
    • 终于让它工作了,我需要先手动运行格式化,然后才能保存。有冲突的 linters 试图对同一个文件进行 lint,但它没有抛出任何错误。如果您安装了多个,上述 sn-p 还需要明确设置您希望它使用哪个 linter。
    • 是的,我也遇到了同样的问题。要设置默认 linter,请打开命令面板 Ctrl+Shift+P 并配置默认格式化程序。
    【解决方案2】:

    您可以安装Prettier Code formatter extension 并将这两个选项添加到您的settings.json 文件中

    {
      "typescript.format.enable": false,
      "[typescript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
      }
    }
    

    【讨论】:

    • 这与我的其他情况不同。
    【解决方案3】:

    使用像“ESLint”这样的插件的替代方法:

    "[typescriptreact]": {
      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 2017-01-22
      • 2020-01-14
      • 2019-05-03
      • 2020-08-27
      • 2023-03-16
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多