如何使用 ESLint 将 vue 模板放置在单独的 html 文件中

Ale*_*rff 5 tooling eslint vue.js prettier

假设您有“SFC”,其模板如下:

<template src="./my-separate.html"></template>
<script>/* whatever */</script>
<style>/* whatever */</style>
Run Code Online (Sandbox Code Playgroud)

假设您已经有了 prettier/eslint 设置,并且可以使用以下命令对任何文件进行 lint 检查:

eslint --ext "*.html, *.js, *.vue, *.ts" $FilePath$ --fix --no-ignore
Run Code Online (Sandbox Code Playgroud)

哪种格式的 .vue、.js 甚至 .ts 文件都很好,但是如果你将它用于分离的 vue-template - 这是一个带有 .html 扩展名的文件,但它实际上是带有所有v-ifs 和其他内容的 vue 模板......这不起作用,因为 prettier 可能尝试使用不正确的解析器来解析 .html (?),或者也许应该有一种方法来建议对我棘手的 .html 文件使用哪个解析器?

我当前的配置如下所示:

// .prettier.rc

{
  "arrowParens": "always",
  "semi": false,
  "singleQuote": true
}

// .eslintrc.js

module.exports = {
  root: true,
  parserOptions: {
    parser: 'typescript-eslint-parser',
    sourceType: 'module',
    jsx: true
  },
  env: {
    browser: true
  },
  extends: [
    'eslint:recommended',
    'plugin:prettier/recommended',
    'prettier/vue',
    'plugin:vue/recommended'
  ],

  plugins: [
    'typescript',
    // required to lint *.vue files
    'vue',
    'prettier'
  ]
}
Run Code Online (Sandbox Code Playgroud)

以及使用的一些库的版本:

"babel-eslint": "8.2.6",
"eslint": "5.11.1",
"eslint-config-prettier": "3.3.0",
"eslint-friendly-formatter": "4.0.1",
"eslint-loader": "2.1.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-node": "8.0.0",
"eslint-plugin-prettier": "3.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-typescript": "0.14.0",
"eslint-plugin-vue": "5.0.0",
Run Code Online (Sandbox Code Playgroud)

也许有人可以告诉我如何使用包含 vue 特定模板的 prettier/eslint .html 文件进行格式化?

目前,对于 .html,我既没有 IDE 错误,也没有自动格式化,但对于其他所有内容(.vue、.js、.ts 文件),它工作正常。

对于我提到的命令,我可以看到 eslint 使用parser-babylon并合理地抱怨第一个与 vue 相关的东西,例如:prop绑定。

UPD: 根据我的经验,如果您尝试将 vue-template 提取为 HTML,那么您已经做错了。这是避免模板复制粘贴的一种方法,但实际上您将使用该方法创建 N 个相同的组件(如果您在 N 个 SFC 中重用该 HTML 块)。所以你最好拥抱 SFC 并尝试制作一个可重用的组件,而不是拥有一个可重用的 HTML 文件。这样,您也不会有任何 linting 问题。

小智 4

问题是 prettier 追随 eslint 插件 vue。根据文档,该插件允许我们使用 ESLint 检查 .vue 文件的 和 。

所以 html 的 linting 不太可能。另请阅读此https://github.com/vuejs/eslint-plugin-vue/issues/490