简体   繁体   English

我无法在我的 vue / nuxt 项目中变得更漂亮和更漂亮

[英]I can't get prettier and eslint to play nicely in my vue / nuxt project

I keep having a problem with ESLint and prettier.我一直有 ESLint 和更漂亮的问题。 It happened several times now that they have conflicting rules and the autoformat on save of one will throw an error on the other.现在发生了好几次,因为它们有冲突的规则,并且保存一个时的自动格式化会在另一个上抛出错误。

My problem at the moment is this line, which has been formatted by ESLint:我目前的问题是这一行,它已被 ESLint 格式化:

<v-card outlined min-width="105" :style="{ backgroundColor: cCodeWaterTemp }">

Then prettier throws this error:然后更漂亮抛出这个错误:

  88:16  error  Replace `·outlined·min-width="105"·:style="{·backgroundColor:·cCodeWaterTemp·}"` with `⏎··········outlined⏎··········min-width="105"⏎··········:style="{·backgroundColor:·cCodeWaterTemp·}"⏎········`  prettier/prettier

Basically saying I should change it into this format基本上是说我应该把它改成这种格式

    <v-card 
    outlined 
    min-width="105" 
    :style="{ backgroundColor: cCodeWaterTemp }"
    >

Which ESLint will then again change on save.哪个 ESLint 将在保存时再次更改。 How can I configure them to have consistent, non conflicting rules?如何将它们配置为具有一致的、不冲突的规则? I went through a few tutorials and at the moment my configuration files look like this我经历了一些教程,目前我的配置文件看起来像这样

Settings.json:设置.json:

{
"window.zoomLevel": 0,
"vetur.validation.template": false,
"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"vetur.completion.scaffoldSnippetSources": {
    "workspace": "",
    "user": "",
    "vetur": ""
},
"prettier.useTabs": true,
"prettier.tabWidth": 4,
"git.autofetch": true,
"[json]": {
    "editor.defaultFormatter": "vscode.json-language-features"
},
"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true
},
"[jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features"
}

eslintrc.js: eslintrc.js:

module.exports = {
    root: true,
    env: {
      node: true,
      browser: true,
    },
    rules: {
      'vue/component-name-in-template-casing': ['error', 'PascalCase'],
      'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
      'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    },
    globals: {
      $nuxt: true,
    },
    parserOptions: {
      parser: 'babel-eslint',
    },
    extends: [
      'plugin:vue/recommended',
      'eslint:recommended',
      'prettier/vue',
      'plugin:prettier/recommended',
      'prettier',
    ],
  }},
  parserOptions: {
    parser: 'babel-eslint',
  },
  extends: [
    'plugin:vue/recommended',
    'eslint:recommended',
    'prettier/vue',
    'plugin:prettier/recommended',
    'prettier',
  ],
}

and editorconfig和编辑器配置

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

{
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": false
  }
}

Any help would be welcome!欢迎任何帮助! Cheers干杯

This is the best way for me.这对我来说是最好的方法。 This works in hot reload too.这也适用于热重载。

In nuxt.config.js addnuxt.config.js添加

buildModules: [
  ...,
  ['@nuxtjs/eslint-module', {fix: true}]
],

In case someone is stumbling across this looking for an answer, I have figured it out by now.万一有人偶然发现这个寻找答案,我现在已经想通了。 I read that the "extends" part inside eslint should be last so no rule in there gets overwritten.我读到 eslint 中的“扩展”部分应该放在最后,所以那里的规则不会被覆盖。 Further, I needed to teach eslint a few tricks regarding vue and prettier which results in this eslint file:此外,我需要教 eslint 一些关于 vue 和 prettier 的技巧,这导致了这个 eslint 文件:

module.exports = {
root: true,
env: {
  node: true,
  browser: true,
},
rules: {
  'vue/component-name-in-template-casing': ['error', 'PascalCase'],
  'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
},
globals: {
  $nuxt: true,
},
parserOptions: {
  parser: 'babel-eslint',
},
extends: [
  'plugin:vue/recommended',
  'eslint:recommended',
  'prettier/vue',
  'plugin:prettier/recommended',
  'prettier',
  'plugin:vue/base',
],
}

Now i just needed to tell the editor that he can correct code on save (.editorconfig):现在我只需要告诉编辑器他可以在保存时更正代码(.editorconfig):

  {
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
  }

And here we go.现在我们开始。 They play nicely for now.他们现在玩得很好。 I found these pages to be a big help:我发现这些页面有很大帮助:

Style Guide to find what's wrong风格指南找出问题所在

Code page to enable the given set of rules in ESLint在 ESLint 中启用给定规则集的代码页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM