You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue项目中如何让</a>闭合标签同行、其余标签换行?解决ESLint冲突

解决Vue项目中a标签闭合换行格式冲突问题

方案1:精准配置ESLint的vue/html-closing-bracket-newline规则

在ESLint配置文件(.eslintrc.js或.eslintrc.json)中,修改规则配置,让多行属性的标签开始闭合符>与内容保持同行:

module.exports = {
  // ...其他配置
  rules: {
    // ...其他规则
    "vue/html-closing-bracket-newline": [
      "error",
      {
        singleline: "never",
        multiline: "never",
        // 可选:只针对特定行内标签生效,比如a、span等
        // ignoreTags: ['a', 'span', 'strong']
      }
    ],
    // 配合调整闭合括号间距,避免多余空格
    "vue/html-closing-bracket-spacing": ["error", {
      startTag: "never",
      endTag: "never",
      selfClosingTag: "always"
    }]
  }
};

方案2:协调ESLint与Prettier的配置冲突

如果同时使用Prettier,需确保两者规则对齐:

  1. 安装eslint-config-prettier禁用冲突规则:
npm install eslint-config-prettier --save-dev
  1. 在ESLint配置中引入该插件:
module.exports = {
  extends: [
    // ...其他继承规则(如plugin:vue/vue3-essential)
    "prettier" // 放在最后覆盖冲突规则
  ],
  // ...其他配置
};
  1. 修改Prettier配置文件(.prettierrc或prettier.config.js):
module.exports = {
  // ...其他配置
  htmlWhitespaceSensitivity: "ignore",
  bracketSameLine: true // 让多行属性标签的闭合符与内容同行
};

验证配置

运行ESLint修复命令测试效果:

npx eslint --fix src/**/*.vue

这样就能全局实现期望的a标签格式,无需逐行添加禁用注释。

内容的提问来源于stack exchange,提问作者karunakaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 06:10:31