简体   繁体   English

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

[英]ESLint: Parsing error: Unexpected token :

Hi everyone I am migrating my vue3 project from js to typescript, I run into this problem:大家好我正在将我的vue3项目从js迁移到typescript,我遇到了这个问题:

在此处输入图像描述

Here is my code in.vue file这是我的代码 in.vue 文件

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

And here is my eslintrc.js这是我的 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']
  }
}

Could someone please help me?有人可以帮我吗? thank you谢谢你

In my case, the problem was that I was using the parser option as an array, instead of a string:在我的例子中,问题是我将解析器选项用作数组,而不是字符串:

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

You need to configure eslint to support typescript as eslint doesn't support it out of the box.您需要配置 eslint 以支持 typescript 因为 eslint 开箱即用不支持它。 First, you need to install @typescript-eslint/parser and then @typescript-eslint/eslint-plugin .首先,您需要安装@typescript-eslint/parser然后@typescript-eslint/eslint-plugin Once you have installed these, simply update your config as follows-一旦你安装了这些,只需更新你的配置如下 -

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']
    }
}

I had this problem on node v12.22.9.我在节点 v12.22.9 上遇到了这个问题。 By upgrading to v14.21.2, I no longer had the parsing error.通过升级到 v14.21.2,我不再有解析错误。 You can upgrade/install with the command您可以使用命令升级/安装

nvm install v14.21.2 

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

相关问题 React typescript eslint 错误解析错误:意外的令牌? 如何解决这个问题? - React typescript eslint error Parsing error: Unexpected token ! How to fix this? ESlint 在 TypeScript 转换运算符“as”上解析错误“意外标记” - ESlint parsing error "Unexpected token" on TypeScript cast operator "as" 当 Eslint 解析 Tsx/Ts 文件 'public' 令牌时,总是显示 'Parsing error: Unexpected token' - When Eslint parse Tsx/Ts file 'public' token, always shows 'Parsing error: Unexpected token' 解析错误:意外的保留字“接口”。 eslint - Parsing error: Unexpected reserved word 'interface'. eslint 错误解析错误:意外的令牌,预期的“,” - error Parsing error: Unexpected token, expected "," 解析错误:意外的令牌,预期的“,” - 与 Typescript 反应 - Parsing error: Unexpected token, expected “,” - React with Typescript 获取解析错误:意外的令牌,预期的“;” - Getting Parsing error: Unexpected token, expected “;” 意外的令牌,预期的“,” Eslint Typescript with Vue - Unexpected token, expected "," Eslint Typescript with Vue Eslint 出现意外的“EnumInvalidMemberName”错误 - Eslint rises unexpected "EnumInvalidMemberName" error 如何修复解析错误:index.tsx 中的意外标记,预期“,”? - How to fix Parsing error: Unexpected token, expected "," in index.tsx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM