typescript - ESLint:解析错误:意外的 token :

标签 typescript vue.js eslint vuejs3

大家好,我正在将我的 vue3 项目从 js 迁移到 typescript,我遇到了这个问题:

enter image description here

这是我在 .vue 文件中的代码

<script setup lang="ts">
const toto = (msg: string) => {
  console.log(msg)
}
</script>

这是我的 eslintrc.js

module.exports = {
  'env': {
    'browser': true,
    'es2021': true
  },
  'extends': [
    'eslint:recommended',
    'plugin:vue/vue3-essential'
  ],
  'parserOptions': {
    'ecmaVersion': 13,
    'sourceType': 'module'
  },
  'plugins': [
    'vue'
  ],
  'rules': {
    'vue/multi-word-component-names': 'off',
    'vue/object-curly-spacing': [2, 'always'],
    'vue/html-closing-bracket-spacing': [2, {
      'selfClosingTag': 'always'
    }],
    'vue/max-attributes-per-line': [2, {
      'singleline': {
        'max': 1
      },
      'multiline': {
        'max': 1
      }
    }],
    'semi': [2, 'never']
  }
}

有人可以帮助我吗?谢谢🙏

最佳答案

您需要配置 eslint 以支持 typescript,因为 eslint 不支持开箱即用。 首先,你需要安装 @typescript-eslint/parser然后 @typescript-eslint/eslint-plugin . 一旦你安装了这些,只需更新你的配置如下-

module.exports = {
    'env': {
        'browser': true,
        'es2021': true,
        node: true
    },
    'extends': [
        'eslint:recommended',
        'plugin:vue/vue3-essential'
    ],
    'parserOptions': {
        'ecmaVersion': 12,
        'sourceType': 'module',
        parser: '@typescript-eslint/parser'
    },
    'plugins': [
        'vue',
        '@typescript-eslint'
    ],
    'rules': {
        'vue/multi-word-component-names': 'off',
        'vue/object-curly-spacing': [2, 'always'],
        'vue/html-closing-bracket-spacing': [2, {
            'selfClosingTag': 'always'
        }],
        'vue/max-attributes-per-line': [2, {
            'singleline': {
                'max': 1
            },
            'multiline': {
                'max': 1
            }
        }],
        'semi': [2, 'never']
    }
}

关于typescript - ESLint:解析错误:意外的 token :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70083042/

相关文章:

javascript - eslint 冗余 double 如何更改为有效

javascript - TypeScript - 如何防止使用构造函数中的变量覆盖类方法

javascript - 数据显示正确,但控制台显示 "Cannot read property ' map '未定义”

javascript - 无法在 typescript 的 JointJS 元素中添加 HTML

javascript - typescript :在特定数量的字符后 trim 其余文本

javascript - Vue js 预取组件

javascript - React 和 Flowtype - 继承类

javascript - 如何从NodeJS获取单个数据并在VueJS中显示?

javascript - Vuejs 类绑定(bind)和类插值

javascript - 使用 prettier 更改现有代码的缩进大小