【问题标题】:Vue and lint on run: how to configure?运行时的 Vue 和 lint:如何配置?
【发布时间】:2019-09-18 04:44:06
【问题描述】:

执行npm run serve时。我从一个 linter 收到了很多我无法找到和配置的警告

Module Warning (from ./node_modules/eslint-loader/index.js):
warning: Insert `;` (prettier/prettier) at src\main.js:1:22:
> 1 | import Vue from "vue"

例如,我的 VsCode 在 Prettier 扩展中设置为使用 4 个空格作为制表符,但是在运行相同的加载程序时会警告我,因为它希望我使用 2 个空格缩进。

我无法确定在哪里/如何配置 eslint-loader 本身以根据需要配置/禁用规则。

warning: Replace `····` with `··` (prettier/prettier) at src\main.js:22:1:
  20 | new Vue({
  21 |     router,
> 22 |     store,
     | ^
  23 |     render: h => h(App)
  24 | }).$mount("#app")

例如,我想禁用此功能,我想强制检查 4 个空格,而不是 2 个空格!

我有 Vetur 扩展,它设置为使用更漂亮

Prettier 设置为使用 4 个空格选项卡。所以我认为我现在需要的设置与vscode无关。

【问题讨论】:

  • 您没有prettierrc 文件或类似的文件吗?
  • 我没有 prettierrc 文件;我读到它被 vscode 忽略了所以我没有尝试。

标签: vue.js visual-studio-code eslint vscode-settings


【解决方案1】:

最终解决方案- 致谢:https://eslint.vuejs.org/user-guide/#editor-integrations,我自己花了很多时间尝试、反复尝试

我禁用了更漂亮的扩展并禁用了 vs 代码的自动格式化。

我将此 sn-p 添加到 workspace 配置中(不是全局的!!!!)

{
    "eslint.validate": [
        {
            "language": "vue",
            "autoFix": true
        },
        {
            "language": "javascript",
            "autoFix": true
        },
        {
            "language": "javascriptreact",
            "autoFix": true
        }
    ],
    "eslint.autoFixOnSave": true,
    "editor.formatOnSave": false,
    "vetur.validation.template": false
}

另外,在 .eslintrc.js 文件上配置 prettier/prettier。

例如,看看我如何使用prettier/prettierrules 部分:

module.exports = {
    root: true,
    env: {
        node: true
    },
    extends: [
        "plugin:vue/recommended",
        "eslint:recommended",
        "prettier/vue",
        "plugin:prettier/recommended",
    ],
    rules: {
        "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
        "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
        "prettier/prettier":[
            "error", 
            {
                "tabWidth"  : 4,
                "semi" : false,
            }
        ]
    },
    parserOptions: {
        parser: "babel-eslint"
    }
}

如我的问题帖子中所述,我在 VsCode 上安装了更漂亮的 eslint 扩展。

在此处查看可用选项:https://prettier.io/docs/en/options.html

通过这种方式,配置既可以在 vscode 上运行,也可以像我想要的那样运行 lint-on-run。

太棒了!

【讨论】:

  • 我们必须创建这个 .eslintrc.js 文件吗?如果是,那么在哪里?
  • .eslintrc.js 文件位于项目的根目录中。它是在你启动一个新项目时由 vue-cli 创建的。
猜你喜欢
  • 2021-02-24
  • 2021-10-09
  • 2022-01-26
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多