vuejs2 - 带有 4 个空格缩进的 Vue-CLI

标签 vuejs2 eslint vue-cli vue-cli-3

我对 Vue 完全陌生。

我想使用 CLI 并且我想要有 4 个空格缩进(使用 typescript 和 vuex)。但是经过 24 小时的挣扎,我离让任何事情都变得更近了。如果这是不可能的,也请告诉我。

我认为 tslint 是要走的路,但找不到解决方案。所以我尝试了 eslint 并将其添加到 package.json

  "devDependencies": {
    "@vue/cli-plugin-eslint": "^3.0.4",
    "@vue/cli-plugin-pwa": "^3.0.4",
    "@vue/cli-plugin-typescript": "^3.0.4",
    "@vue/cli-service": "^3.0.4",
    "@vue/eslint-config-prettier": "^3.0.4",
    "@vue/eslint-config-typescript": "^3.0.4",
    "eslint": "^5.6.1",
    "eslint-plugin-vue": "^5.0.0-beta.3",      // <------------------
    "typescript": "^3.0.0",
    "vue-template-compiler": "^2.5.17"
  }

然后是 eslintrc 我有
 module.exports = {
    root: true,
    env: {
        node: true
    },
    extends: ["plugin:vue/essential", "@vue/prettier", "@vue/typescript"],
    rules: {
        "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
        "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
        "vue/script-indent": ["error", 4, { baseIndent: 1 }],
        indent: ["error", 4]
    },
    parserOptions: {
        parser: "typescript-eslint-parser"
    }
};

然后当我跑
$ npx eslint --fix *.js
=============

WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: ~3.0.1

YOUR TYPESCRIPT VERSION: 3.1.1

Please only submit bug reports when using the officially supported version.

=============

/Users/x/code/sync/vue/restos2/postcss.config.js
  2:1  error  Expected indentation of 4 spaces but found 2  indent
  3:1  error  Expected indentation of 8 spaces but found 4  indent
  4:1  error  Expected indentation of 4 spaces but found 2  indent

✖ 3 problems (3 errors, 0 warnings)
  3 errors and 0 warnings potentially fixable with the `--fix` option.
but the problem is that no files ever get changed.

最佳答案

如 tslint doc 中所述,缩进规则不会修复错误数量的缩进字符,它只会修复字符类型,这意味着它可以将制表符转换为空格,反之亦然,但它不会将 4 个空格修复为 2 个空格

NOTE: auto-fixing will only convert invalid indent whitespace to the desired type, it will not fix invalid whitespace sizes.



https://palantir.github.io/tslint/rules/indent/

所以是的,你必须使用 eslint。我没有看到你完整的 eslintrc 文件,但我认为主要问题可能是这一行:indent: ["error", 4] .尝试删除它。

这个 eslint 配置对我有用:
{
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended",
      "@vue/typescript"
    ],
    "rules": {
      "vue/script-indent": ["error",2,{"baseIndent": 1}]
    },
    "parserOptions": {
      "parser": "typescript-eslint-parser"
    }
}

并使用 npm run lint 运行

关于vuejs2 - 带有 4 个空格缩进的 Vue-CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52707751/

相关文章:

javascript - Vue路由器从默认 View 返回时清除历史记录API?

vue.js - vuejs如何使用v-for只迭代数组的一部分

eslint 返回错误但 Github 操作仍然成功

javascript - 我如何告诉 Vue-cli 我的应用程序的入口点在哪里?

vue.js - Vue-cli 3 - Material 设计图标安装

vue.js - 使用目标 wc 构建选项时,vue-cli 不会将代码转换为 es5 或 commonjs

javascript - Vue 和 Nuxt Js : why Cannot create property 'display' on string 'bottom:30px;right:30px;' ?

typescript - Redis 客户端中的 Promisify 违反了 eslint 规则

typescript - 如何防止 "any"在 TypeScript 类型和接口(interface)中使用

javascript - Vue 将一个对象作为计算属性展开