【问题标题】:ESLint: Parsing error: Unexpected token :ESLint:解析错误:意外令牌:
【发布时间】:2022-01-02 01:45:22
【问题描述】:

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

这是我在 .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.io/docs/linting/linting

标签: typescript vue.js eslint vuejs3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-12-25
    • 2018-08-14
    • 2017-11-21
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多