在 VS Code 中保存时如何格式化代码 [英] How do you format code on save in VS Code

查看:47
本文介绍了在 VS Code 中保存时如何格式化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Visual Studio Code 中保存文件时,我想使用内置格式化程序自动格式化 TypeScript 代码.

I would like to automatically format TypeScript code using the build-in formatter when I save a file in Visual Studio Code.

我知道以下选项,但它们都不够好:

I'm aware of the following options, but none of them is good enough:

  • 手动格式化Shift + Alt + F
  • 格式"editor.formatOnType": true
    • 当您按 Enter 键时,它会格式化该行.不幸的是,当您单击另一行或按向上/向下箭头时,它会使其未格式化.
    • 我尝试了这个 one,但似乎没有工作太好了.
    • I tried this one, but it does not seem to work too well.
    • 它不适用于 TypeScript
    • 如果您想正确处理自动保存和构建,这很棘手.

    推荐答案

    截至 2016 年 9 月(VSCode 1.6),现在正式 支持.

    As of September 2016 (VSCode 1.6), this is now officially supported.

    将以下内容添加到您的 settings.json 文件:

    Add the following to your settings.json file:

    "editor.formatOnSave": true
    

    这篇关于在 VS Code 中保存时如何格式化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆