![](/img/trans.png)
[英]React typescript eslint error Parsing error: Unexpected token ! How to fix this?
[英]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.