首页 新闻 会员 周边 捐助

VSCode 扩展 Prettier 总是使用 singleAttributePerLine 方式格式化 html

0
悬赏园豆:30 [已解决问题] 解决于 2025-06-23 22:50

在 vscode 的配置文件 settings.json 将默认 html formatter 设置为 prettier

{
    "[html]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
}

格式化 html 代码时总是将 html tag 的每个 attribute 单独放在一行

对应的配置选项没有勾选

.prettierrc.js 中添加了下面的设置

singleAttributePerLine: false,

但问题依旧,请问如何解决这个问题?

问题补充:

与 stackoverflow 上的这个问题是同一个问题 How do you stop Prettier in VS code splitting attributes onto multiple lines?

设置 Print Width,问题依旧

dudu的主页 dudu | 高人七级 | 园豆:24705
提问于:2025-06-23 18:56
< >
分享
最佳答案
0

终于在 github issue It is not possible to disable singleAttributePerLine with JSX 的评论中找到了解决方法

需要在 .prettierrc 配置文件中给 printWidth 设置一个更大的值,每个 attribute 是否单独放在一行是由 printWidth 决定的,超过 printWidth,singleAttributePerLine 的设置就不起作用

{
  "overrides": [
    {
      "files": ["**/*.html"],
      "options": {
        "printWidth": 150
      }
    }
  ]
}
dudu | 高人七级 |园豆:24705 | 2025-06-23 22:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册