简体   繁体   English

Vue/TypeScript/ESLint/Prettier/Vetur 格式化不起作用

[英]Vue/TypeScript/ESLint/Prettier/Vetur formatting doesn't work

Trying to get Vue/TypeScript/ESLint/Prettier/Vetur formatting in VS Code is a nightmare.试图在 VS Code 中获取 Vue/TypeScript/ESLint/Prettier/Vetur 格式是一场噩梦。 There are many many GitHub issues and StackOverflow posts on this but I assure you this is not a duplicate.有很多 GitHub 问题和关于此的 StackOverflow 帖子,但我向您保证这不是重复的。 I have followed every tutorial and none of them work.我已经按照每个教程进行操作,但没有一个起作用。 Some of them fix one problem but introduce another.其中一些解决了一个问题,但引入了另一个问题。 Some of them don't fix any problems.其中一些不能解决任何问题。 Some of them crash VS Code.其中一些使 VS Code 崩溃。 Most conflict with each other in the advice they prescribe, including multiple official sources.大多数人在他们开出的建议中相互冲突,包括多个官方来源。 Many are outdated, referencing obsolete config properties.许多已经过时,引用过时的配置属性。

I want VS Code to lint and format my.vue and.ts files when I save.我希望 VS Code 在保存时对 my.vue 和 .ts 文件进行 lint 和格式化。

I have spent hours and tried many, many configurations from different posts and tutorials, but this is the closest I have gotten to something that works.我花了几个小时并尝试了来自不同帖子和教程的许多配置,但这是我最接近可行的配置。 With the below configuration, however, whenever saving a.vue file, elements in the.vue files get momentarily wrapped onto a new line, and then immediately reverted back to a single line element:但是,使用以下配置,无论何时保存 .vue 文件,.vue 文件中的元素都会暂时换行,然后立即恢复为单行元素:

在此处输入图像描述 then然后在此处输入图像描述 then然后在此处输入图像描述

Below are my current configuration files:以下是我当前的配置文件:

.eslintrc.js .eslintrc.js

const { resolve } = require('path');
module.exports = {
  root: true,
  parserOptions: {
    extraFileExtensions: ['.vue'],
    parser: '@typescript-eslint/parser',
    project: resolve(__dirname, './tsconfig.json'),
    tsconfigRootDir: __dirname,
    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module' // Allows for the use of imports
  },

  env: {
    browser: true
  },

  extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', 'plugin:vue/recommended'],

  plugins: ['@typescript-eslint', 'vue'],

  globals: {
    ga: true, // Google Analytics
    cordova: true,
    __statics: true,
    process: true,
    Capacitor: true,
    chrome: true
  },

  rules: {
    'prettier/prettier': ['error', { usePrettierrc: true }], 
    'prefer-promise-reject-errors': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-empty-function': ['error', { allow: ['private-constructors'] }],
    'vue/no-v-html': 'off',
    'vue/no-template-shadow': 'off',
    'vue/max-attributes-per-line': 'off',
    quotes: ['warn', 'single', { avoidEscape: true }],

    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
};

.prettierrrc .prettierrrc

{
  "singleQuote": true,
  "semi": true,
  "useTabs": false,
  "printWidth": 150,
  "arrowParens": "avoid",
  "trailingComma": "none",
  "endOfLine": "auto"
}

settings.json设置.json

{
  "editor.formatOnPaste": true,
  "editor.formatOnSave": true,
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "vue"
  ],
  "typescript.tsdk": "node_modules/typescript/lib",
  "vetur.experimental.templateInterpolationService": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },
  "editor.detectIndentation": false,
  "editor.tabSize": 2
}

Does anyone out there actually have this working?有没有人真的有这个工作?

To get Vue/TypeScript/ESLint/Prettier/Vetur working in VSCode, I followed the following steps:为了让 Vue/TypeScript/ESLint/Prettier/Vetur 在 VSCode 中工作,我遵循了以下步骤:

  1. Installed the Vue eslint plugin by running vue add @vue/cli-plugin-eslint .通过运行vue add @vue/cli-plugin-eslint安装 Vue eslint 插件。 NOTE: If you have a client folder directory and a server folder directory, you must first cd to your client directory before using vue add .注意:如果您有一个客户端文件夹目录和一个服务器文件夹目录,您必须先 cd 到您的客户端目录,然后再使用vue add
  2. Changed the .eslintrc.js in my Vue project.更改了我的 Vue 项目中的.eslintrc.js Please check this post for more information/alternatives.请查看这篇文章以获取更多信息/替代方案。 Basically, I made a new Vue + Typescript project and copied the eslint config to my real project.基本上,我创建了一个新的 Vue + Typescript 项目并将 eslint 配置复制到我的真实项目中。 The boilerplate code didn't work completely at first so I modified it a bit:样板代码一开始并不能完全工作,所以我对其进行了一些修改:
module.exports = {
  root: true,
  parser: "vue-eslint-parser",
  parserOptions: {
    parser: "@typescript-eslint/parser",
  },
  env: {
    browser: true,
  },
  extends: [
    "plugin:@typescript-eslint/recommended",
    "plugin:vue/recommended",
    "@vue/prettier",
    "plugin:prettier/recommended",
    "@vue/typescript",
  ],
  // required to lint *.vue files
  plugins: [
    "vue",
    "@typescript-eslint"
  ],
  // add your custom rules here
  rules: {
    // allow async-await
    "generator-star-spacing": "off",
    // allow debugger during development
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
    "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
}
  1. Installed the following dependencies by running通过运行安装以下依赖项
npm i -D eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard

For more information, please check out this Github page.有关更多信息,请查看Github 页面。 NOTE: I did not need eslint-plugin-promise (it threw an error when I tried installing it, but it might work for you).注意:我不需要eslint-plugin-promise (当我尝试安装它时它抛出了一个错误,但它可能对你有用)。

  1. Changed my VSCode settings.json to the following:将我的 VSCode settings.json更改为以下内容:
{
    "editor.defaultFormatter": "octref.vetur",
    "vetur.format.defaultFormatter.html": "prettier",
    "vetur.format.defaultFormatter.css": "prettier",
    "vetur.format.defaultFormatter.postcss": "prettier",
    "vetur.format.defaultFormatter.scss": "prettier",
    "vetur.format.defaultFormatter.less": "prettier",
    "vetur.format.defaultFormatter.stylus": "stylus-supremacy",
    "vetur.format.defaultFormatter.js": "prettier",
    "vetur.format.defaultFormatter.ts": "prettier",
    "[json]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "editor.codeActionsOnSave": [
        "source.organizeImports",
        "source.fixAll"
    ],
    "editor.formatOnSave": false,
    "javascript.format.enable": false,
    "eslint.alwaysShowStatus": true,
    "eslint.options": {
    "extensions": [ ".html", ".js", ".vue", ".jsx", ".ts" ]
    },
    "eslint.validate": [ "javascript", "vue", "html", "typescriptvue", "typescript" ],
    "eslint.workingDirectories": [
        "./client"
    ]
}

And with that, I'm able to use Vue/TypeScript/ESLint/Prettier/Vetur.这样,我就可以使用 Vue/TypeScript/ESLint/Prettier/Vetur。 It lints and formats my code perfectly each save and I don't really have to worry about the tiny things in my code.每次保存时它都会完美地检查和格式化我的代码,我真的不必担心我的代码中的小东西。 Please let me know if you still have trouble and I'll try to see what I can do.如果您仍然遇到问题,请告诉我,我会尝试看看我能做些什么。

I've been having some conflict issues between @vue/prettier and eslint.我一直在@vue/prettier 和 eslint 之间遇到一些冲突问题。 Removing @vue/prettier the problem disappeared.删除@vue/prettier 问题就消失了。 After researching the package I realized that it was a temporary solution until prettier supported Vue natively, and it already does .在研究了 package 之后,我意识到这是一个临时解决方案,直到 prettier 原生支持 Vue,并且它已经支持了。

I honestly don't know why this package is still recommended by installing vue-cli but I tested putting 'prettier' in place of '@vue/prettier' and it worked老实说,我不知道为什么这个 package 仍然通过安装 vue-cli 被推荐,但我测试了用 'prettier' 代替 '@vue/prettier' 并且它有效

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

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