繁体   English   中英

Vue中使用的接口的Eslint no-unused-vars错误显示

[英]Eslint no-unused-vars error shows for used interface in Vue

我将 Vue 与 Vue CLI 和 Typescript 一起使用。
我从 vuex 文件导入接口并将其用于mapState类型注释。
但是 eslint 向我显示了一个错误。

'State' is defined but never used. eslint(no-unused-vars)


代码

import { Component, Vue } from 'vue-property-decorator';
import { mapState } from 'vuex';
import { State } from '@/store/index';

@Component({
  computed: mapState<State>({
    cards: (state: State) => state.user.cards,
  })
})
export default class Home extends Vue {}

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true
  },
  'extends': [
    'plugin:vue/essential',
    'eslint:recommended',
    '@vue/typescript'
  ],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'quotes': ['error', 'single'],
    'semi': ['warn', 'always']
  },
  parserOptions: {
    parser: '@typescript-eslint/parser'
  }
};


我应该怎么做才能看到 eslint 错误?

解决方案:

EsLint 默认no-unused-vars有一个错误,会像您解释的那样重现。 根据我的消息来源 [1],它应该通过使用@typescript-eslint/no-unused-vars来修复,如下所示:

overrides: [
   // Fix no-used-vars when importing ts types in .vue files
   {
     files: ["*.vue"],
     rules: {
        'no-unused-vars': 'off',
        '@typescript-eslint/no-unused-vars': 'error'
     }
  }
]

来源 [2] 是关于同一主题的另一个问题。 在那里,我的解决方案 [1] 是其中一种选择。 在 [2] 和 [3] 中,据说您需要像这样安装 @ @typescript-eslint/eslint-plugin@typescript-eslint/parser

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

资源:

[1] https://github.com/vuejs/eslint-config-typescript/issues/14

[2] ESLint - 为 TypeScript 配置“no-unused-vars”

[3] https://khalilstemmler.com/blogs/typescript/eslint-for-typescript/

暂无
暂无

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

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