如何在VS Code中开启数组链式方法自动换行格式化功能?
This auto-formatting behavior is handled by code formatter tools, with Prettier being the most common choice (used by many instructors in coding courses). Here's how to set it up in VS Code (the most popular editor for JavaScript):
Install the Prettier Extension
- Open VS Code, go to the Extensions tab (Ctrl+Shift+X / Cmd+Shift+X)
- Search for "Prettier - Code formatter" (published by esbenp) and install it.
Turn On Format-On-Save
- Open Settings (Ctrl+, / Cmd+,)
- Search for "Editor: Format On Save" and check the box to enable it.
Set Prettier as Default Formatter
- In Settings, search for "Editor: Default Formatter"
- Select "esbenp.prettier-vscode" from the dropdown list for JavaScript files.
Customize Chained Method Formatting (Optional)
By default, Prettier will split chained array methods onto new lines when the line exceeds the defaultprintWidth(80 characters). If you want to force every chained method to be on its own line regardless of length:- Install the ESLint extension (published by dbaeumer)
- Create a
.eslintrc.jsonfile in your project root with these rules:{ "extends": ["eslint:recommended", "plugin:prettier/recommended"], "rules": { "newline-per-chained-call": ["error", { "ignoreChainWithDepth": 1 }] } }
This ESLint rule enforces a newline after each chained method call, ensuring your array chains are always neatly formatted.
Note: Alt+Z is just a word-wrap feature, which doesn't perform code formatting—this is why it didn't solve your issue.
内容的提问来源于stack exchange,提问作者Rohan Kumar

