今天在创建vue项目得时候使用了eslint,然后npm安装了prettier,设置prettierrc的规则,但是格式化并没有按规则来,在vscode里设置了默认格式化程序半天也没弄好,这时根据一个解答———在prettier插件设置中取消勾选editor的一个选项,我项目里没有这个文件,就想试一试添加.editorconfig文件,配置如下:
# http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
然后那些eslint报错就没有了,格式化也好了,还可以在在package.json中配置一个scripts:
"prettier": "prettier --write ."
就可以 npm run prettier 进行所有代码格式化了,按照.prettierrc的配置。
如果不生效,可以试试在vscode的setting.json中添加下面试一下。记得配置好重启vscode。
"editor.formatOnSave": false, // 禁止保存的时候格式化
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},