简体   繁体   English

defineEmits 的 Vue linting 错误 function

[英]Vue linting errors for defineEmits function

I'm having an issue with the linting for my Vue SPA.我的 Vue SPA 检测有问题。 I'm using the defineEmits function from the script setup syntactic sugar ( https://v3.vuejs.org/api/sfc-script-setup.html ).我正在使用脚本设置语法糖 ( https://v3.vuejs.org/api/sfc-script-setup.html ) 中的 defineEmits function。 The errors just do not make any sense, does anyone know how to fix this (without disabling these rules for the affected files, because it happens to every usage of defineEmits).这些错误没有任何意义,有没有人知道如何解决这个问题(没有为受影响的文件禁用这些规则,因为它发生在每次使用 defineEmits 时)。 The weird thing is that the defineProps function works without errors, which follows the same syntax.奇怪的是 defineProps function 没有错误,它遵循相同的语法。 Can anyone help me out here?有人可以帮我从这里出去吗?

My linter complains about these errors:我的 linter 抱怨这些错误:

22:14  error  Unexpected space between function name and paren       no-spaced-func
22:27  error  Unexpected whitespace between function name and paren  func-call-spacing
23:3   error  'e' is defined but never used                          no-unused-vars
23:27  error  'value' is defined but never used                      no-unused-vars

Code that is generating these errors (the defineEmits is the source of all the errors:产生这些错误的代码(defineEmits 是所有错误的来源:

<script lang="ts" setup>
const emit = defineEmits<{
    (e: 'update:modelValue', value: string): void
}>()

defineProps<{
    modelValue: string
    name: string
    items: string[]
}>()

const onInput = (e: Event) => {
    emit('update:modelValue', (e.target as HTMLInputElement).value)
}
</script>

My linting eslintrs.js file (the imported shared rules do not modify the rules eslint is complaining about):我的 linting eslintrs.js 文件(导入的共享规则不会修改 eslint 抱怨的规则):

const path = require('path')

const prettierSharedConfig = require(path.join(__dirname, '../prettier-shared-config.json'))

module.exports = {
    settings: {
        'import/resolver': {
            typescript: {},
            node: {
                extensions: ['.js', '.ts', '.vue'],
            },
        },
    },
    env: {
        browser: true,
        es2021: true,
        'vue/setup-compiler-macros': true,
    },
    extends: ['plugin:vue/essential', 'airbnb-base'],
    parserOptions: {
        ecmaVersion: 13,
        parser: '@typescript-eslint/parser',
        sourceType: 'module',
    },
    plugins: ['vue', '@typescript-eslint'],
    rules: {
        ...prettierSharedConfig.rules.shared,
        'vue/multi-word-component-names': 'off',
        'vue/no-multiple-template-root': 'off',
    },
}

Update:更新:

I did some further digging and saw this happening:我做了一些进一步的挖掘,看到了这种情况:

type EmitsType = {
    (e: 'update:modelValue', value: string): void
}

const emit = defineEmits<EmitsType>()

With the following linting output:使用以下 linting output:

23:3   error  'e' is defined but never used      no-unused-vars
23:27  error  'value' is defined but never used  no-unused-vars

It seems that the linter cannot properly process these types.看起来 linter 无法正确处理这些类型。

I had the same issue, I've found 2 solutions, it fixes this problem but I'm not pretty sure if I was doing it wrong.我有同样的问题,我找到了 2 个解决方案,它解决了这个问题,但我不太确定我是否做错了。

  1. Add '@typescript-eslint/recommended' to your eslintrc'@typescript-eslint/recommended'添加到您的eslintrc
  plugins: [
    ...,
    '@typescript-eslint/recommended',
  ],

or或者

  1. Add 'func-call-spacing' rule添加'func-call-spacing'规则
  rules: {
    ...
    'func-call-spacing': 'off', // Fix for 'defineEmits'
  }

Here's the additional details about the no-unused-vars .这是有关no-unused-vars的其他详细信息。

https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/no-unused-vars.md https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/no-unused-vars.md

You should use @typescript-eslint/no-unused-vars for your eslint rule.您应该将@typescript-eslint/no-unused-vars用于您的 eslint 规则。 It will show the correct result.它将显示正确的结果。

"rules": {
    "@typescript-eslint/no-unused-vars": "error",
}

I did not find an ideal answer, but my current workaround is to use a different defineEmits syntax.我没有找到理想的答案,但我目前的解决方法是使用不同的 defineEmits 语法。

defineEmits(['update:modelValue'])

The downside of this is does not type the emit parameter value, but I could not find another way to make it work.这样做的缺点是不输入 emit 参数值,但我找不到另一种让它工作的方法。

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

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