对于每个typescript文件,visual studio代码使用8个空格的自动缩进。这对我来说有点太贵了,但我找不到换的地方。

也许它可以作为一个设置,但在不同的名称下,因为我找不到任何与缩进有关的东西。

更新

我目前正在使用更漂亮的代码格式化器,它解决了所有的格式问题,自动格式化保存(如果没有语法错误)


当前回答

首先,检查你是否安装了“EditorConfig for VS Code”。它覆盖了我的编辑器设置。我花了一整天修正这个问题。

在项目中找到.editorconfig文件,并在那里更改它将工作。

其他回答

除了elliott - j的答案之外,您可能还希望将editor.detectIndentation设置为false。

VSCode会覆盖你的编辑器。tabSize和编辑器。如果它检测到文件具有不同的制表符或空格缩进模式,则为每个文件添加insertSpaces设置。如果你将现有文件添加到项目中,或者使用Angular Cli等代码生成器添加文件,就会遇到这个问题。上面的设置阻止VSCode这样做。

在我的案例中,“EditorConfig for VSCode”扩展覆盖了VSCode设置。 如果您已经安装了它,那么检查项目根文件夹中的.editorconfig文件。

下面是一个配置示例。“indent_size”设置制表符的空格数。

# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

首先,检查你是否安装了“EditorConfig for VS Code”。它覆盖了我的编辑器设置。我花了一整天修正这个问题。

在项目中找到.editorconfig文件,并在那里更改它将工作。

我喜欢这些缩进设置,你可以根据需要修改它们。

你可以打开VScode设置。输入CTRL+SHIFT+P并粘贴到json设置下面

setting.json

"[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features",
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": false,
    "editor.detectIndentation": false,
    "editor.wrappingIndent": "deepIndent", 
    "editor.autoIndent": "full"
},
"[typescript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features",
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": false,
    "editor.detectIndentation": false,
    "editor.wrappingIndent": "deepIndent", 
    "editor.autoIndent": "full"
}

要设置所有现有文件和新文件的空格标识为2,只需把它放在你的设置。Json(在Json的根):

"[typescript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features",
        "editor.tabSize": 2,
        "editor.insertSpaces": true,
        "editor.detectIndentation":false
 }

您可以添加配置的语言类型:

"[javascript]": {
    "editor.tabSize": 2,
    "editor.insertSpaces": true,
    "editor.detectIndentation":false
}