使用 webstorm 运行 vue 项目时。报错如下:
WARNING Compiled with 1 warnings
✘ http://eslint.org/docs/rules/indent Expected indentation of 0 spaces but found 2
src\components\NewHello.vue:8:1
export default {
✘ http://eslint.org/docs/rules/indent Expected indentation of 2 spaces but found 4
src\components\NewHello.vue:10:1
data() {
✘ http://eslint.org/docs/rules/space-before-function-paren Missing space before function parentheses
src\components\NewHello.vue:10:9
data() {
✘ 3 problems (3 errors, 0 warnings)
Errors:
2 http://eslint.org/docs/rules/indent
1 http://eslint.org/docs/rules/space-before-function-paren
该报错是由于 Vue 项目初始化时,默认生成的验证规则(eslintrc.js)文件验证失败导致。
报错信息很清楚。
1、缩进允许 0 个空格,但是发现了 2 个;
2、缩进允许 2 个空格,但是发现了 4 个;
3、方法插入之前丢失了空格
因为在 eslint 中。默认的规则下,script 内容首行不缩进,内容依照层次一次缩进 2 个空格,方法和括号之间需要一个空格。
而编辑器的格式化可能与 eslint 的规则不太一致,所以导致了如上报错。
解决的办法也很简单。
有两种:
1、修改 IDEA 的设置,将 JS 文件的 Formatter 规则修改与 eslint 的检验规则一致。
2、修改 eslint 的检验规则,忽略对缩进、空格等并不重要的一些规则的验证。(推荐)
进入 eslintrc.js 文件。在 rules 中,加入以下三个规则,忽略对缩进、空格的检查:
'no-tabs':'off',
'indent':'off',
'space-before-function-paren':0
重新运行
npm run dev
DONE Compiled successfully in 5142ms
I Your application is running here: http://localhost:8080