繁体   English   中英

ESLint:解析错误:意外的令牌:

[英]ESLint: Parsing error: Unexpected token :

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

在此处输入图像描述

这是我的代码 in.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']
  }
}

有人可以帮我吗? 谢谢你

在我的例子中,问题是我将解析器选项用作数组,而不是字符串:

   parserOptions: {
-    parser: ['@typescript-eslint/parser'],
+    parser: '@typescript-eslint/parser',
   },

您需要配置 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']
    }
}

我在节点 v12.22.9 上遇到了这个问题。 通过升级到 v14.21.2,我不再有解析错误。 您可以使用命令升级/安装

nvm install v14.21.2 

暂无
暂无

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

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