我已经为TypeScript和Vue使用了很长时间的以下预设。它是有效的,但我还没有理解每一个选项,现在我将理解它。首先:parser
和@typescript-eslint/parser
之间有什么区别?
parser: vue-eslint-parser
parserOptions:
parser: "@typescript-eslint/parser"
sourceType: module
project: tsconfig.json
tsconfigRootDir: ./
extraFileExtensions: [ ".vue" ]
env:
es6: true
browser: true
node: true
plugins:
- "@typescript-eslint"
- vue
实验数据
如果没有parser: "vue-eslint-parser"
,我们在TypeScript文件的第行中有[unknown]: Parsing error: Unexpected token :
:
(async function executeApplication(): Promise<void> {})()
和.vue
文件中的Parsing error: Unexpected token <
,行:
<template lang="pug">
如果我们删除或注释掉parserOptions.parser: "@typescript-eslint/parser"
,
[unknown]: Parsing error: Unexpected token :
将保留Parsing error: Unexpected token <
将消失,而Parsing error: Unexpected character '@'
将出现在@Component export default class extends Vue {
行中
是否需要parser
和@typescript-eslint/parser
?
vue-eslint-parser
是要使用的主解析器,而不是默认解析器(espree
(。它将处理.vue SFC文件,尤其是<template>
标记。
在这个解析器中,您有一个自定义选项来指定要使用哪个解析器来整理.vue
文件中的<script>
标记。
因此,基本上,您是在告诉esint使用vue-eslint-parser
来解析.vue
文件,并且在这个解析器中,使用@typescript-eslint/parser
作为<script>
标记。