【问题标题】:how to eslint with Vue components?如何使用 Vue 组件进行 eslint?
【发布时间】:2019-01-23 13:24:38
【问题描述】:

我有一个定义组件的 .vue 文件。 对于 linter,其他 .vue 文件似乎无法正确看到它。 我收到许多 ES Lint 错误,例如:

Cannot find module '../components/LinkButton'.

我已经完成了诸如

之类的步骤
  1. 在我的组件上方添加一个自定义// @vue/component https://github.com/vuejs/eslint-plugin-vue#attention

  2. 确保不包含 html 插件 https://github.com/vuejs/eslint-plugin-vue#why-doesnt-it-work-on-vue-file

  3. 最新的 eslint 和插件

"eslint": "^5.3.0", "eslint-plugin-vue": "^5.0.0-beta.3",

不知道还有什么可以尝试的。我的客户端 .eslintrc 如下。

module.exports = {
  root: true,
  env: {
    browser: true,
  },

  "parserOptions": {
    "parser": "babel-eslint",
    "ecmaVersion": 2017,
    "sourceType": "module"
  },
  // extends: 'vue',
  extends: 'plugin:vue/base',

  // required to lint *.vue files
  plugins: [
    'vue',
  ],
  // add your custom rules here
  'rules': {
    'space-before-function-paren': "off",
    'no-multi-spaces': 'off',
    // allow paren-less arrow functions
    'arrow-parens': 0,
    // allow async-await
    'generator-star-spacing': 0,
    "no-multiple-empty-lines": 0,
    "comma-dangle": 0,
    "padded-blocks": 0,
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
  }
}
```

组件定义如下

<script>
// @vue/component
const LinkButton = {
  name: 'LinkButton',
  props: [
    'label',
    'link',
    'type',
    'icon'
  ],
  data () {
    return {
      msg: 'NLP testing'
    }
  }
}

module.exports = {LinkButton}

</script>

并导入

import {LinkButton} from '../components/LinkButton'

请注意,该文件使用 webpack 编译正常,这只是一个 linting 问题。

谢谢!

【问题讨论】:

  • 为什么不直接使用 es2017 的导出默认/导入语法?
  • 我也试过了,同样的问题。我认为更明确可能会有所帮助,但没有......

标签: javascript vue.js vuejs2 eslint


【解决方案1】:

eslint 配置的parser 属性应该在根目录下,而不是在parserOptions 内。

module.exports = {
  // ...
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2017,
    "sourceType": "module"
  }
  // ...

也没有必要在plugins 中指定vue,因为它已经由继承的plugin:vue/base 配置完成。

还要注意configuration file format must be specified in the file name。

配置文件格式

ESLint 支持多种格式的配置文件:

  • JavaScript - 使用 .eslintrc.js 并导出包含您的配置的对象。
  • YAML - 使用.eslintrc.yaml 或.eslintrc.yml 定义配置结构。
  • JSON - 使用.eslintrc.json 定义配置结构。 ESLint 的 JSON 文件还允许 JavaScript 样式的 cmets。
  • 已弃用 - 使用 .eslintrc,可以是 JSON 或 YAML。
  • package.json - 在您的 package.json 文件中创建一个 eslintConfig 属性并在那里定义您的配置。

在您的情况下,您应该将文件命名为 .eslintrc.js,因为您正在导出节点模块。

为了让 eslint 正确加载 Vue 文件,就像使用相同的 webpack 配置一样,需要在 eslint 配置文件中明确指定 import/resolver 设置。

settings: {
    'import/resolver': {
        webpack: {
            config: 'webpack.conf.js', // if at the project root
        },
    },
},

这是由eslint-import-resolver-webpack 模块使用的,它也应该被安装。

【讨论】:

  • 嗯,这改变了事情,但更糟。现在,linter 在任何类似 Vue 的代码上都失败了,例如 &lt;el-tree :props="treeProps" 它会将 :props 冒号标记为意外字符...
  • @dcsan 我已经添加了更多信息来帮助使用默认配置,下一步是调整规则,使其仅对您需要的 linting 进行 lints。
  • 导入/解析器的东西去哪儿了?进入 .eslint.js 或某个地方的 webpack 配置文件? FWIW 我正在使用来自quasar-framework.org 的相当复杂的模板
  • @dcsan 需要在eslint配置中指定webpack配置在哪里,这样ESLint才能验证import语句。
猜你喜欢
  • 2022-08-09
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2020-09-23
  • 2016-10-30
  • 1970-01-01
  • 2019-06-25
相关资源
最近更新 更多