Vue HTML 中的错误格式

时间:2021-07-02 11:20:51

标签: html vue.js vue-component prettier

我从 Vue 开始,我已经配置了 ESLint 和 Prettier 以使用我自己的风格来格式化代码,但它没有按预期工作。当我想在模板中打印一个变量时,VS Code 中的格式化程序做了一些奇怪的事情(我附上了一些代码)

我希望为 msg(它是一个字符串)保持这种格式

<template>
    <div class="test">
        <ul>
            {{ msg }}
        </ul>
    </div>
</template>

但是我得到了这个(格式化程序在 msg 行中插入了 2 个换行符)

<template>
    <div class="test">
        <ul>
            {{
                msg
            }}
        </ul>
    </div>
</template>

我使用更漂亮的 (.prettierrc.json)

{
    "useTabs": true,
    "tabWidth": 2,
    "endOfLine": "lf",
    "arrowParens": "always",
    "semi": true,
    "singleQuote": true,
    "trailingComma": "none",
    "htmlWhitespaceSensitivity": "ignore"
}

和 ESLint (.eslintrc.js)

module.exports = {
    env: {
        browser: true,
        es6: true,
        node: true
    },
    extends: ['plugin:vue/essential', 'standard', 'prettier'],
    globals: {
        Atomics: 'readonly',
        SharedArrayBuffer: 'readonly'
    },
    parserOptions: {
        ecmaVersion: '2020',
        sourceType: 'module'
    },
    plugins: ['vue']
};

我已尝试在 VS Code 中禁用格式设置,但无法解决此问题。

有谁知道我该如何解决这个“问题”?

1 个答案:

答案 0 :(得分:0)

对我来说最好只使用 ESlint ,下面你可以找到我的配置

module.exports = {
    env: {
        'browser': true,
        'es6': true,
        'node': true
    },
    parserOptions: {
        'parser': 'babel-eslint'
    },
    extends: [
        '@vue/standard',
        'plugin:vue/recommended'
    ],
    rules: {
        "vue/html-indent": ["error", 4, {
            "attribute": 1,
            "closeBracket": 0,
            "switchCase": 0,
            "ignores": []
        }],

        "vue/max-attributes-per-line": [2, {
            "singleline": 10,
            "multiline": {
              "max": 1,
              "allowFirstLine": false
            }
          }],
        'indent': ['error', 4],
        'vue/component-name-in-template-casing': ['error', 'kebab-case'],
        'vue/script-indent': [
            'error',
            4,
            { 'baseIndent': 1 }
        ],
        'space-before-function-paren': ['error', 'never'],
        'semi': [2, "never"],
        'vue/singleline-html-element-content-newline': 'off',
        'vue/multiline-html-element-content-newline': 'off'
    },
    overrides: [
        {
            'files': ['*.vue'],
            'rules': {
                'indent': 'off'
            }
        }
    ]
}

很多时候,prettier 会限制你的 eslint 格式,你会遇到问题,最好用一个视觉修复程序配置你的程序。尝试使用我给您的 eslint 格式并禁用更漂亮的格式来格式化您的项目,然后告诉我问题是否仍然存在!